CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2006
    Posts
    170

    Question xmldocument and nodes

    Hello!

    I want to make a xmlstructure like this:

    Code:
    <?xml version="1.0" ?> 
      <ROOT>
          This is the text of the root element 
           <SupplierID>
                 Supplier id
                 <SupplierName>Supplier name</SupplierName> 
           </SupplierID>
      </ROOT>
    This is my code so far:

    Code:
      xmldoc = new XmlDocument();
                //let's add the XML declaration section
                xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xmldoc.AppendChild(xmlnode);
    
                //let's add the root element
                xmlelem = xmldoc.CreateElement("", "ROOT", "");
                xmltext = xmldoc.CreateTextNode("This is the text of the root element");
    
                xmlelem.AppendChild(xmltext);
                xmldoc.AppendChild(xmlelem);
    
                //let's add another element (child of the root)
                xmlelem2 = xmldoc.CreateElement("", "SupplierID", "");
                xmltext = xmldoc.CreateTextNode("Supplier id");
                xmlelem2.AppendChild(xmltext);
                xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);
    now my problem I don't know how to add a node into my first node SupplierID?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: xmldocument and nodes

    Try using the CreateStartElement() method when creating a parent node like that. Then use CreateElement() to append elements to the current parent node. Don't forget to call EndStartElement() for each CreateStartElement() call.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: xmldocument and nodes

    If you need to serialize/unserialize some data structures, you can use XmlSerializer. There is also a new set of classes for handling XML documents, XElement. For simple files like yours I think it's much easier to use.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured