Results 1 to 11 of 11

Thread: Write XML using XmlDocument

  1. #1
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959

    Write XML using XmlDocument

    I've reached a point where it's time to learn how to Read/Write XML files properly and I'm really confused to how we're supposed to do this. I've been reading up on using the XmlDocument, but all I'm finding is how to use it to read xml files. Right now I need to Write them, later I'll code the reading of them.

    The data for the XML file comes from a couple of tables in a database too, I've got a Master/Child table (actually there's 5 child tables). I've got the data coming into DataTables and I've got the loops set up already, I'm just stuck to how do I take the Master table's row and store that data as an XmlNode then loop each of the 5 child DataTable's and add each row as a child node to the master's node.

    Anyone know of any examples along this line?
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  2. #2
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    This is how I start one:
    Code:
     Dim sXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
     sXML = String.Concat(sXML, Environment.NewLine, "<Root>")
     sXML = String.Concat(sXML, Environment.NewLine, "<FirstElement />")
     sXML = String.Concat(sXML, Environment.NewLine, "</Root>")
     Dim clsDocument As New XmlDocument
     clsDocument.PreserveWhitespace = True
     clsDocument.LoadXml(sXML)
     clsDocument.Save("your.xml")
    Last edited by newguy; 03-31-2010 at 10:34 AM.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  3. #3
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    Quote Originally Posted by newguy View Post
    This is how I start one it:
    Code:
     Dim sXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
     sXML = String.Concat(sXML, Environment.NewLine, "<Root />")
     sXML = String.Concat(sXML, Environment.NewLine, "<FirstElement />")
     Dim clsDocument As New XmlDocument
     clsDocument.PreserveWhitespace = True
     clsDocument.LoadXml(sXML)
     clsDocument.Save("your.xml")
    So, in essence you use a StringBuilder and manually create the tags on the fly then push it out?

    Wouldn't there be a better way to achieve this?

    Though I am noticing a lack of being able to do this:
    Code:
    Dim XmlDoc As New XmlDocument
    
    'A Do or a For loop of some sort here
    
    Dim MasterNode As New XMLNode("NameOfNode")
    Dim ChildNode1 As New XMLNode("NameOfChildNode") 'There'd be the 5 total
    
    With MasterNode
        .Attributes.Add(New XmlAttribute("NameOfAttribute", AttributeDataType, AttributeValue)
        .Attributes.Add(New XmlAttribute("NameOfAttribute", AttributeDataType, AttributeValue)
        .Attributes.Add(New XmlAttribute("NameOfAttribute", AttributeDataType, AttributeValue)
        'etc..
    End With
    With ChildNode1
        .Attributes.Add(New XmlAttribute("NameOfAttribute", AttributeDataType, AttributeValue)
        'etc..
    End With
    
    MasterNode.Nodes.Add(ChildNode1)
    
    XmlDoc.Nodes.Add(MasterNode)
    
    'Loop to the next MasterRow
    
    Dim xw As New XmlWriter("FilePath")
    xw.Write(XmlDoc)
    xw.Close
    Obviously this is just pseudopod, but you should get the picture. This is why I'd like an article to follow as what I posted above doesn't seem to come close to how it actually works and I'd like to know how it actually works.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  4. #4
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    I edited my 1st post - corrected.
    This is just something I use to get started and then create new elements - be it loops or single ones.

    Code:
    <rootnode>.AppendChild(xmldoc.CreateElement("name"))
    There probably is a better way, but this is what I know. Of course Datasets read/write xmls.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  5. #5
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    Looks like I need to fiddle with the XmlTextWriter: Create xml file Samples and examples - C#, VB.NET, ASP.NET
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  6. #6
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    I found that to be more complicated than the XmlDocument - but that's me. Also you don't need an xmlWriter, the XmlDocument has a Save method when you are done changing, adding, deleting elements.

    One the cool things about the XmlDocument, it's in-cache memory, you can make tons of changes to the document and at the end Save or not (reverts back to what it was previously).
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  7. #7
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    That's what I would like to do, build the whole thing at once then save the end result when all the loops are done.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  8. #8
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    OK, here is an example, if you have something more specific let me know.

    Code:
    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
        Dim sXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
        sXML = String.Concat(sXML, Environment.NewLine, "<Root>")
        sXML = String.Concat(sXML, Environment.NewLine, "     ", "<One />")
        sXML = String.Concat(sXML, Environment.NewLine, "     ", "<Two />")
        sXML = String.Concat(sXML, Environment.NewLine, "     ", "<Three />")
        sXML = String.Concat(sXML, Environment.NewLine, "</Root>")
        Dim clsDocument As New XmlDocument
        clsDocument.PreserveWhitespace = True
        clsDocument.LoadXml(sXML)
        For i As Integer = 1 To 10
         
          clsDocument.SelectSingleNode("//Two").AppendChild(clsDocument.CreateElement("x" & i.ToString))
        Next
        clsDocument.SelectSingleNode("//One").Attributes.Append(clsDocument.CreateAttribute("test"))
        clsDocument.Save("your.xml")
    End Sub
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  9. #9
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    571
    Any reason you wouldn't be able to use LINQ to XML and XML Literals?

    Pretty good example of using related tables to handle this here: Beth Massi on XML Literals in VB.NET

    The video is ~1 hour long but the part you want to check out is around the 17 min mark. (I would recommend the whole thing as it's all good information.)

  10. #10
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    Quote Originally Posted by MattP View Post
    Any reason you wouldn't be able to use LINQ to XML and XML Literals?

    Pretty good example of using related tables to handle this here: Beth Massi on XML Literals in VB.NET

    The video is ~1 hour long but the part you want to check out is around the 17 min mark. (I would recommend the whole thing as it's all good information.)
    The client only has the 1.1 and 2.0 Frameworks installed.

    Win98SE can't even run the 3.0 or 3.5 Frameworks so LINQ is out of the picture for a long time still. I'm soo glad VS2008 can target the 2.0 Framework.

    newguy:
    check out this post: VBForums - View Single Post - VS 2005 [RESOLVED] Write XML using XmlDocument I'm currently using the loops I'd created previously and am now writing the Element start, then the field contents followed by the end element for each section so I now have:
    Code:
    <document start>
    <master record start>
      <master record field>
      <master record field>
      <child record start>
        <child record field>
        <child record field>
      <child record end>
      <child record start>
        <child record field>
        <child record field>
      <child record end>
    <master record end>
    
    'Multiple master records...
    
    <document end>
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  11. #11
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    571
    Quote Originally Posted by JuggaloBrotha View Post
    The client only has the 1.1 and 2.0 Frameworks installed.

    Win98SE can't even run the 3.0 or 3.5 Frameworks so LINQ is out of the picture for a long time still. I'm soo glad VS2008 can target the 2.0 Framework.
    Too bad, I find that with XML Literals and embedded expressions you end up with a more legible & maintainable result.

    Using loops with a XmlTextWriter will get you the results that you're looking for though.

Bookmarks

Posting Permissions

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