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.
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");
}
}
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:
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.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.