473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Insert XML string into Document

Hi all -
I need some help trying to insert / append a block of xml text into an xml
document. This is for a reporting app and as new data is available, I must
add it to the end of the document. It should be simple but I cannot seem to
find the magic combo. Here is what I am trying to do:

The XML String is periodically generated and indicates the results of a
system test. The File is the Log File and is opened and a new XmlDocument is
created using the Log File FileStream. I want to insert / append the XML
string in the ResultsLog Tags. I have tried a number of variations but can't
seem to hit the right approach. The XML string is generated internally and
cannot be changed. Any suggestions are appreciated.
// Open the stream and read it back.

FileStream* fs = File::Open(sTestLogFile, FileMode::OpenOrCreate,
FileAccess::ReadWrite, FileShare::None);

XmlDocument* xmlReport = new XmlDocument;

xmlReport->Load(fs);
XML file:

<?xml version="1.0"?>
<ResultsLog>
</ResultsLog>

XML String:

<TestResults>
<Description>Unit number 2</Description>
<Tests>
<Test>
<Name type="string">REF to DUT Test</Name>
<Duration type="integer">40</Duration>
<Attenuation type="integer">70</Attenuation>
<Direction type="integer">0</Direction>
<MinRate type="integer">0</MinRate>
<MaxRate type="integer">200</MaxRate>
</Test>
<Test>
<Name type="string">REF to DUT Test</Name>
<Duration type="integer">40</Duration>
<Attenuation type="integer">71</Attenuation>
<Direction type="integer">0</Direction>
<MinRate type="integer">0</MinRate>
<MaxRate type="integer">200</MaxRate>
</Test>
<Test>
<Name type="string">REF to DUT Test</Name>
<Duration type="integer">40</Duration>
<Attenuation type="integer">72</Attenuation>
<Direction type="integer">0</Direction>
<MinRate type="integer">0</MinRate>
<MaxRate type="integer">200</MaxRate>
</Test>
</Tests>
</TestResults>
May 19 '06 #1
7 2463


S Wheeler wrote:

FileStream* fs = File::Open(sTestLogFile, FileMode::OpenOrCreate,
FileAccess::ReadWrite, FileShare::None);

XmlDocument* xmlReport = new XmlDocument;

xmlReport->Load(fs);


pseudo code:
XmlDocumentFragment* fragment = xmlReport->CreateDocumentFragment();
fragment.InnerXml = stringGoesHere;
xmlReport->DocumentElement->AppendChild(fragment);
// now output or save back
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 19 '06 #2
Based on your description, what I get it that you want to insert the
XML string into the ResultsLog element. If that is the case then it is
pretty straightforward.

XmlReport.Load(fs);
XmlReport.DocumentElement.InnerXml+=<your xml string here>;
XmlReport.Save(<save stream>);
The above code is in C#. You can easily convert it into C++;

Note: This will work only if the xml string to be inserted is well
formed and valid. Else an XmlException will be thrown.

Also if ResultsLog is not your root element, then change the second
line to
XmlReports.SelectSingleNode(<XPath to ReportsLog node>).InnerXml

-Dhanvanth

May 19 '06 #3


Dhanvanth wrote:

XmlReport.Load(fs);
XmlReport.DocumentElement.InnerXml+=<your xml string here>;
XmlReport.Save(<save stream>); Note: This will work only if the xml string to be inserted is well
formed and valid. Else an XmlException will be thrown.


Validity is certainly not checked if you add to InnerXml or do other DOM
manipulations.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 19 '06 #4
Thanks Martin -

That worked - sort of.... the appendchild seems to add another <?xml
version="1.0"?> and instead of my xml being inside of the
<ResultsLog></ResultsLog> tags it is in a new <ResultsLog></ResultsLog> tag
pair. Any ideas?
Thanks


"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:en**************@TK2MSFTNGP05.phx.gbl...


S Wheeler wrote:

FileStream* fs = File::Open(sTestLogFile, FileMode::OpenOrCreate,
FileAccess::ReadWrite, FileShare::None);

XmlDocument* xmlReport = new XmlDocument;

xmlReport->Load(fs);


pseudo code:
XmlDocumentFragment* fragment = xmlReport->CreateDocumentFragment();
fragment.InnerXml = stringGoesHere;
xmlReport->DocumentElement->AppendChild(fragment);
// now output or save back
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

May 19 '06 #5
That seems to introduce a "<?xml version="1.0"?>" is there any way to
prevent this?
"Dhanvanth" <dh*******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Based on your description, what I get it that you want to insert the
XML string into the ResultsLog element. If that is the case then it is
pretty straightforward.

XmlReport.Load(fs);
XmlReport.DocumentElement.InnerXml+=<your xml string here>;
XmlReport.Save(<save stream>);
The above code is in C#. You can easily convert it into C++;

Note: This will work only if the xml string to be inserted is well
formed and valid. Else an XmlException will be thrown.

Also if ResultsLog is not your root element, then change the second
line to
XmlReports.SelectSingleNode(<XPath to ReportsLog node>).InnerXml

-Dhanvanth

May 19 '06 #6


S Wheeler wrote:

That worked - sort of.... the appendchild seems to add another <?xml
version="1.0"?> and instead of my xml being inside of the
<ResultsLog></ResultsLog> tags it is in a new <ResultsLog></ResultsLog> tag
pair. Any ideas?


Is there an <?xml version="1.0"?> in the snippet of XML you want to
insert? Your example did not have one and AppendChild will not create
such stuff. And it will not add a new ResultsLog element unless it has
been in the snippet of XML you parse. I read your original request as
that you simply want to insert everything you have in the string.
Here is an example (using C#):

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(@"<ResultsLog />");
// output for test
xmlDocument.Save(Console.Out);
Console.WriteLine();
Console.WriteLine();

string exampleXML = @"<TestResults>
<Description>Unit number 2</Description>
<Tests>
<Test>
<Name type=""string"">REF to DUT Test</Name>
<Duration type=""integer"">40</Duration>
<Attenuation type=""integer"">70</Attenuation>
</Test>
</Tests>
</TestResults>";
XmlDocumentFragment fragment = xmlDocument.CreateDocumentFragment();
fragment.InnerXml = exampleXML;
xmlDocument.DocumentElement.AppendChild(fragment);

// output for test
xmlDocument.Save(Console.Out);

The complete output on the console is

<?xml version="1.0" encoding="utf-8"?>
<ResultsLog />

<?xml version="1.0" encoding="utf-8"?>
<ResultsLog>
<TestResults>
<Description>Unit number 2</Description>
<Tests>
<Test>
<Name type="string">REF to DUT Test</Name>
<Duration type="integer">40</Duration>
<Attenuation type="integer">70</Attenuation>
</Test>
</Tests>
</TestResults>
</ResultsLog>

so neither is any <?xml version="1.0"?> XML declaration added nor is
there any new ResultsLog element made up.

Maybe the original XML sample you showed is not what you finally used
but then you need to post what you have so that we can suggest the
proper code.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 20 '06 #7
I agree. Well formedness was what i wanted to specify. Oversight on my
part to add validity there.

May 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony B | last post by:
Hi I need to insert a new node into an exisiting XML file using VBScript. The file has the following structure: <TRANSACTION> <ORDER> <BILLING>... </BILLING> <ORDER_TOTALS>... </ORDER_TOTALS>...
4
by: Sergio del Amo | last post by:
i, I have the next html page <html> <head> <script> <!-- function insertcode() { var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfafa<a...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
1
by: a | last post by:
I am using following code to insert text into the word document, using VB from excel. Any text which has length more than 240 characters is not getting inserted into the document. Any...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
11
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString);...
2
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
9
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...
0
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.