> I'm creating some XML documents in my C# .net 2.0 code. To date I've
> mostly only read XML. Now I'm writing it and I'm struggling to figure
[quoted text clipped - 8 lines]
> with XMLWriter such that I can add child elements to an existing XML
> file or is it more for creating XML docs from scratch in one shot?

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin-
Thanks for that. I was playing with XMLDocument late last night :-). I guess
my question is, what is the general approach for adding new nodes? For
example, I have two nested elements in my existing document. I want to add
my new elements as children of the inner element. So, do I first create a
reference to the parent element where I want to start my adds and then
simply start doing AppendChild? Also, once I've added a new element, do I
have to position the cursor explicitly on that element to add, for example,
a CDATA section to it? The reason I ask is that my biggest problem is not
adding new nodes, but it putting them where I want them. It seems to be all
over the map, based on my limited experience.
Thanks,
Darren
>> I'm creating some XML documents in my C# .net 2.0 code. To date I've
>> mostly only read XML. Now I'm writing it and I'm struggling to figure out
[quoted text clipped - 16 lines]
> CreateNavigator and use the various edit method XPathNavigator allows. See
> <http://msdn2.microsoft.com/en-us/library/20esef39(VS.80).aspx>
Martin Honnen - 30 Aug 2007 13:19 GMT
> Thanks for that. I was playing with XMLDocument late last night :-). I
> guess my question is, what is the general approach for adding new nodes?
[quoted text clipped - 6 lines]
> biggest problem is not adding new nodes, but it putting them where I
> want them. It seems to be all over the map, based on my limited experience.
AppendChild/InsertBefore/InsertAfter need to be called on the new parent
node so you are right to select that parent first (e.g. with
SelectSingleNode) to be able to call AppendChild or InsertBefore or
InsertAfter.
As for providing the contents of newly created elements (with .NET) you
simply do that like this:
XmlElement element = xmlDocumentInstance.CreateElement("element-name");
element.InnerText = "element content";
newParentElement.AppendChild(element);
If you want to add a CDATA section then you can't set InnerText but
simply do e.g.
XmlElement element = xmlDocumentInstance.CreateElement("element-name");
element.AppendChild(xmlDocumentInstance.CreateCDATASection(
"foo & bar"));
newParentElement.AppendChild(element);
So there is no cursor concept, you are dealing with nodes that have
properties like InnerText and InnerXml and methods like AppendChild or
InsertBefore. With newly created nodes there is no need to position
anything as the methods like CreateElement return a reference to the
node and you can then apply methods and set properties directly.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Darren Mar-Elia - 31 Aug 2007 02:40 GMT
Thanks Martin-
I did end up figuring this out. I have to admit it was less intuitive than
other aspects of the framework but in the end, I got it to do what I wanted.
Thanks again for your hints in the right direction!

Signature
Darren
>> Thanks for that. I was playing with XMLDocument late last night :-). I
>> guess my question is, what is the general approach for adding new nodes?
[quoted text clipped - 32 lines]
> anything as the methods like CreateElement return a reference to the node
> and you can then apply methods and set properties directly.
Martin Honnen - 31 Aug 2007 13:25 GMT
> I did end up figuring this out. I have to admit it was less intuitive
> than other aspects of the framework but in the end, I got it to do what
> I wanted.
With .NET 3.5 (respectively Visual Studio 2008) you will have LINQ and
LINQ to XML which is supposed to be more intuitive than DOM. But
currently it is only in beta.
The beta documentation is online here:
<URL:http://msdn2.microsoft.com/en-us/library/bb387098(VS.90).aspx>
Creating an element with text content is then as "easy" as e.g.
XElement newElement = new XElement("element-name", "element content");
Creating an element with child elements then works as follows:
XElement newElement = new XElement("element-name",
new XElement("child-name", "foo"),
new XElement("child-name", "bar")
);

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