Thanks to some good help from a previous post, I have been able to create
well formed xml as part of a report logger app. However, I still have a
small problem. When I add new xml to the log file, the new nodes are
appended to the original file. What I really want is for the log file to
grow as "report nodes" are added. Also, I am a bit concerned about
performance, particularly as the file grows in size. I must write to an xml
file (vs. a database) and am trying to understand the best way to do this. I
prefer to not use "temp files" and would like to just modify the log file
structure "in-place". Any advice is appreciated. Also, can anyone recommend
a good book that covers XML issues when programming with Managed VC++? The
reports I am generating will eventually be analyzed using Excel or some
other tools such as Crystal Reports.
The follow is a test app that illustrates the problem.
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll >
#using <System.dll>
#using <System.Xml.dll >
//
using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
int _tmain()
{
FileStream* fs;
XmlDocument* xmlReport;
// Test XML Snippet - emulates a test outcome
String* xmlStr = S"<TestResult>< Name>Applicatio n Load
Test</Name><StartTime >21/05/2006
14:45:58:155</StartTime><Stop Time>21/05/2006
14:46:35:530</StopTime><TestR esult>FAIL</TestResult><Tes tAtoms><TestAto m><Name>RateTes t
1</Name><StartTime >21/05/2006 14:46:20:530</StartTime><Stop Time>21/05/2006
14:46:35:530</StopTime><TestR esult>FAIL</TestResult><Dur ation>15</Duration><Direc tion>0</Direction><MinR ate>255</MinRate><MaxRat e>255</MaxRate></TestAtom></TestAtoms></TestResult>";
// the log file
String* sTestLogFile = S"D:\\Developme nt\\LogTest\\Re sultLog.xml";
//
try {
if(File::Exists (sTestLogFile) == true) {
// file already exist
fs = File::Open(sTes tLogFile, FileMode::Open, FileAccess::Rea dWrite,
FileShare::None );
xmlReport = new XmlDocument;
// Open from Stream
xmlReport->Load(fs);
XmlDocumentFrag ment* docFrag = xmlReport->CreateDocument Fragment();
docFrag->InnerXml = xmlStr;
XmlNode* ResultsNode =
xmlReport->DocumentElemen t->SelectSingleNo de("TestResults ");
// ResultsNode->AppendChild(do cFrag);
ResultsNode->PrependChild(d ocFrag);
//xmlReport->DocumentElemen t->PrependChild(p arentNode);
} else {
// no log file currently exist so we create a new file
fs = File::Open(sTes tLogFile, FileMode::Creat eNew, FileAccess::Rea dWrite,
FileShare::None );
xmlReport = new XmlDocument();
XmlDeclaration* xmlDeclaration = xmlReport->CreateXmlDecla ration(S"1.0",
S"utf-8", NULL);
// Create the root element
XmlElement* rootNode = xmlReport->CreateElement( "TestLog");
xmlReport->InsertBefore(x mlDeclaration, xmlReport->DocumentElemen t);
xmlReport->AppendChild(ro otNode);
// Create a new <TestResults> element and add it to the root node
XmlElement* parentNode = xmlReport->CreateElement( "TestResult s");
xmlReport->DocumentElemen t->PrependChild(p arentNode);
parentNode->InnerXml = xmlStr;
}
xmlReport->Save(fs);
fs->Close();
}
catch(XmlExcept ion* e)
{
String* msg = e->get_Message( );
}
catch(Exception * e)
{
String* msg = e->get_Message( );
}
return 0;
}
Output when no file exist GOOD:!
<?xml version="1.0" encoding="utf-8"?>
<TestLog>
<TestResults>
<TestResult>
<Name>Applicati on Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTes t 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
</TestResults>
</TestLog>
Output when file exist (see above) BAD!
<?xml version="1.0" encoding="utf-8"?>
<TestLog>
<TestResults>
<TestResult>
<Name>Applicati on Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTes t 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
</TestResults>
</TestLog><?xml version="1.0" encoding="utf-8"?>
<TestLog>
<TestResults>
<TestResult>
<Name>Applicati on Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTes t 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
<TestResult>
<Name>Applicati on Load Test</Name>
<StartTime>21/05/2006 14:45:58:155</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<TestAtoms>
<TestAtom>
<Name>RateTes t 1</Name>
<StartTime>21/05/2006 14:46:20:530</StartTime>
<StopTime>21/05/2006 14:46:35:530</StopTime>
<TestResult>FAI L</TestResult>
<Duration>15</Duration>
<Direction>0</Direction>
<MinRate>255</MinRate>
<MaxRate>255</MaxRate>
</TestAtom>
</TestAtoms>
</TestResult>
</TestResults>
</TestLog> 4 2057
SteveW wrote: FileStream* fs; XmlDocument* xmlReport; // Test XML Snippet - emulates a test outcome
String* sTestLogFile = S"D:\\Developme nt\\LogTest\\Re sultLog.xml"; // try { if(File::Exists (sTestLogFile) == true) { // file already exist fs = File::Open(sTes tLogFile, FileMode::Open, FileAccess::Rea dWrite, FileShare::None ); xmlReport = new XmlDocument; // Open from Stream xmlReport->Load(fs);
I think the problem is not with the DOM code (e.g.
XmlDocument.Cre ateXXX, AppendChild, PrependChild) you use but simply
with that file stream you use and reuse. Above if the Load call is done
the stream is read and is subsequently positioned at its very end and
then when you do
xmlReport->Save(fs);
the Save call writes to the end of the stream and you will get the
markup duplicated.
So you need to ensure that your Save call overwrites an existing file
and does not append to it.
One way to do that would be (pseudo code)
String* sTestLogFile = S"D:\\Developme nt\\LogTest\\Re sultLog.xml";
if(File::Exists (sTestLogFile) == true) {
xmlReport = new XmlDocument();
xmlReport.Load( sTestLogFile)
}
xmlReport.Save( sTestLogFile);
so not using any FileStream at all but using the higher level overloads
of the Load and Save methods which simply take a string with a file name.
--
Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Also, it seems that you haven't gotten rid of the problem of the "<?xml
version="1.0" encoding="utf-8"?> " being inserted every time you append
an XML fragment to your file.
Cerebrus wrote: it seems that you haven't gotten rid of the problem of the "<?xml version="1.0" encoding="utf-8"?> " being inserted every time you append an XML fragment to your file.
If he uses the suggestion I made then there will be no duplicated XML
declaration. Currently his codes safes the complete XML document
(including the XML declaration and all other nodes) to the end of the
stream instead of overwriting the same file.
--
Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Martin -
Your advice really helped. I use a file stream when the file does not exist
to create a new file; but when the file does exist, I use the log file path
to load the XMLDocument as you suggested. This works well and I consider the
problem resolved. Thanks MVP's for all the good help! I am really interested
in digging into this more - can anyone recommend a good book that details
all the various XML methods? I primarily use managed vc++, so something with
a emphases on vc++ is best.
"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:eG******** ********@TK2MSF TNGP04.phx.gbl. ..
SteveW wrote:
FileStream* fs; XmlDocument* xmlReport; // Test XML Snippet - emulates a test outcome
String* sTestLogFile = S"D:\\Developme nt\\LogTest\\Re sultLog.xml"; // try { if(File::Exists (sTestLogFile) == true) { // file already exist fs = File::Open(sTes tLogFile, FileMode::Open, FileAccess::Rea dWrite, FileShare::None ); xmlReport = new XmlDocument; // Open from Stream xmlReport->Load(fs);
I think the problem is not with the DOM code (e.g. XmlDocument.Cre ateXXX, AppendChild, PrependChild) you use but simply with that file stream you use and reuse. Above if the Load call is done the stream is read and is subsequently positioned at its very end and then when you do
xmlReport->Save(fs);
the Save call writes to the end of the stream and you will get the markup duplicated. So you need to ensure that your Save call overwrites an existing file and does not append to it.
One way to do that would be (pseudo code)
String* sTestLogFile = S"D:\\Developme nt\\LogTest\\Re sultLog.xml"; if(File::Exists (sTestLogFile) == true) {
xmlReport = new XmlDocument(); xmlReport.Load( sTestLogFile) }
xmlReport.Save( sTestLogFile);
so not using any FileStream at all but using the higher level overloads of the Load and Save methods which simply take a string with a file name.
--
Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: 3c273 |
last post by:
Hello,
I have a really simple Access database table with a format similar to this:
CustomerName - ProductOrdered - QtyOrdered
I have a CSV file with the appropriate values as follows:
Customer1, Widget1, 1000
Customer2, Widget2, 3000
etc
I have figured out how to insert the data manually from the interactive
|
by: jason |
last post by:
How does one loop through the contents of a form complicated by dynamic
construction of checkboxes which are assigned a 'model' and 'listingID' to
the NAME field on the fly in this syntax: Hunter_69. Here is what the form
looks like. I have the difficulty of inserting the multiple items selected
by the user the first time he visits and uses the screen and then using an
UPDATE when he visits later.
Model | Original Price | Reduced Price...
|
by: me |
last post by:
I'm also having problems getting the bulk insert to work. I don't know
anything about it except what I've gleened from BOL but I'm not seeming to
get anywhere...Hopefully there is some little (or big) problem with my code
that someone can point out that may save me some time.
TIA
CBL
|
by: Bill |
last post by:
I'm having what seems to me to be an odd problem. Perhaps there is some
explanation, but don't know at this point. Basically I have a form that
tracks memberships and donations. The main form tracks the individual and
the subform allows me to add donation amounts or membership fee payments.
It's fairly basic. Well what I want to do is when I enter a new membership
payment it looks to another table. If the person is currently a member,...
|
by: tano |
last post by:
Hello,
I have to insert a char in the middle of a string, I have written two functions
but I don't know what is the better? The problem is: if I use malloc() I copy
all the string with the new char in the middle every time, with realloc() the
part of the string before the position where the char has to be inserted is not
changed if realloc returns the same pointer is passed, but if not the
string is copied at all the first time, and then...
| |
by: Ekhaat |
last post by:
Hi
I followed the Web Matrix guided tour and came to the "ASP.NET Pages with
Data
(Microsoft Access)" part.
There is really not much you can do wrong there, but for some reason, the
INSERT part gives me an error. Here is the function that fails (appart from
the exception handling everything is generated by the code wizard):
|
by: NuB |
last post by:
I have a sql query that is doing an update of records, how can I add NULL to
the field in the database if the field on my screen is blank?
example:
I have 5 textboxes, and a user can leave some blank, delete data from a text
box then hit update, how can I have NULL inserted into the field on the
database instead of having a blank record in the db
|
by: anachronic_individual |
last post by:
Hi all,
Is there a standard library function to insert an array of characters
at a particular point in a text stream without overwriting the existing
content, such that the following data in appropriately moved further
down? From a cursory search of the libc documentation I can't find such
a function. Will I have to write it myself?
Thanks.
|
by: cricrin |
last post by:
Hello guys!
This is Cristian From Argentina, and I wanted to ask you some help,
I've looking on this and it makes me mad, I found that the error is in
the $content , when y try to insert the record into the table via PHP
code i receive an error message saying that the sin taxis its
incorrect,however if i print the query echo ($query) and i copy and
paste the query in the PHPmyAdmin the insert executes successfully.
the $content variable...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |