Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / August 2007

Tip: Looking for answers? Try searching our database.

guidance on writing XML

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darren Mar-Elia - 29 Aug 2007 00:00 GMT
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 the best
ways to do it and the best classes to use. Essentially I'm starting with a
XML file that contains the root and start and end elements and I want to add
instances of a child element to the file. Since XMLWriter is forward-only,
it seemed to not be the best tool but maybe I'm misunderstanding it. I then
moved on to XPathNavigator but am stumbling trying to add some CDATA. I've
googled for some code samples but haven't turned anything definitive up. Any
links to some good examples or just general guidance on this? Can I position
the cursor 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?

Thanks

Darren
Martin Honnen - 29 Aug 2007 12:19 GMT
> 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?

If you have an existing XML document and want to manipulate it then
System.Xml.XmlDocument and the DOM implementation is the right tool,
load the existing document with the Load method, then use methods like
CreateElement and InsertBefore or AppendChild to create and insert new
elements. As XmlDocument implements IXPathNavigable you can also use
CreateNavigator and use the various edit method XPathNavigator allows.
See <http://msdn2.microsoft.com/en-us/library/20esef39(VS.80).aspx>

Signature

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

Darren Mar-Elia - 29 Aug 2007 17:10 GMT
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/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.