Click to See Complete Forum and Search --> : Writing and Appending XML with C#
dfb78
March 26th, 2003, 09:33 AM
Does anyone have any simple examples of writing to an XML document, and then also opening and appending to it after "drilling" down a couple elements? Everything I have found displays information on how to create and write an XML document, which I have down. I can't, however, find anything on reopening the document and then adding more elements and/or data to it.
Thanks
SimonVega
March 26th, 2003, 10:44 AM
Here you go:
using System;
using System.Xml;
class XmlIO
{
static void Main()
{
//sample.xml is a simple xml document that exists
//in the root directory of the executable
XmlDocument source = new XmlDocument();
source.Load("sample.xml");
Console.WriteLine(source.DocumentElement.SelectSingleNode("make").InnerText);
XmlElement node = source.CreateElement("model");
node.InnerText = "tt";
source.DocumentElement.AppendChild(node);
Console.WriteLine(source.DocumentElement.SelectSingleNode("model").InnerText);
source.Load("sample.xml");
}
}
dfb78
March 26th, 2003, 11:16 AM
Thanks!
dfb78
March 31st, 2003, 11:10 AM
I think I may be missing something here. I am able to load the XML document, and do what looks to be a drill-down into the root element, but it isn't working. For instance, with the sample document below:
==============================================
<?xml version="1.0" ?>
<!-- Saved Calculations for further reference -->
<SavedCalculations>
<Item>
<ItemName>Sample Item Name</ItemName>
<ItemValue>10.43</ItemValue>
</Item>
</SavedCalculations>
==============================================
I just want to drill down to the 'SavedCalculations' element, and then add a new <Item> with its corresponding <ItemName> and <ItemValue>, so it would now look like this:
==============================================
<?xml version="1.0" ?>
<!-- Saved Calculations for further reference -->
<SavedCalculations>
<Item>
<ItemName>Sample Item Name</ItemName>
<ItemValue>10.43</ItemValue>
</Item>
<Item>
<ItemName>Sample Item Name2</ItemName>
<ItemValue>46.03</ItemValue>
</Item>
</SavedCalculations>
==============================================
Would the example you gave perform this if I modified it like this:
==============================================
XmlDocument source = new XmlDocument();
source.Load("sample.xml");
Console.WriteLine(source.DocumentElement.SelectSingleNode("SavedCalculations").InnerText);
XmlElement node = source.CreateElement("Item");
XmlElement node = source.CreateElement("ItemName");
node.InnerText = "Saved Item Name2";
source.DocumentElement.AppendChild(node);
XmlElement node = source.CreateElement("ItemValue");
node.InnerText = "46.03;
source.DocumentElement.AppendChild(node);
Console.WriteLine(source.DocumentElement.SelectSingleNode("model").InnerText);
source.Load("sample.xml");
==============================================
Thanks!
dfb78
March 31st, 2003, 11:21 AM
Sorry...just double-checked, and the code actually looks like this:
XmlDocument source = new XmlDocument();
source.Load(m_SavedCalculationDocument);
XmlElement node = source.CreateElement("Item");
XmlElement node2 = source.CreateElement("ItemName");
node.InnerText = "Sample Item Name2";
source.DocumentElement.AppendChild(node2);
XmlElement node3 = source.CreateElement("ItemValue");
node.InnerText = "46.03";
source.DocumentElement.AppendChild(node3);
source.Load(m_SavedCalculationDocument);
dfb78
April 1st, 2003, 06:24 AM
Never mind...just got it. :D
pareshgh
April 1st, 2003, 10:43 AM
could you tell us how did you got that. was busy such a long 2-3 days.
;)
apreciate if you show us your solution.
Paresh
dfb78
April 1st, 2003, 11:07 AM
I pretty much used this online application's source and modified the AddContact() method to accomodate my specific needs:
http://www.mastercsharp.com/article.aspx?ArticleID=69&&TopicID=9
The entire XML.NET area of this site looks to be down right now though, so you may want to check back later.
For those who are looking to utilize XML functinoality via C#, this is an excellent example with comments. I learned just about as much with it as I did in my C# book's explanation and examples with XML.
pareshgh
April 1st, 2003, 11:11 AM
oh ! I din't know that's an XML site.
i will check back later.
glad to hear back from you very soon :D ;)
Paresh
dfb78
April 1st, 2003, 12:27 PM
Actually, it looks like the entire site is down. I just remembered that I pulled the source and readme for the app earlier today though, so I have attached it for those who are interested in taking a look.
pareshgh
April 1st, 2003, 12:31 PM
thanks for your attachment. and hope you keep posting good code.
Paresh
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.