Results 1 to 11 of 11

Thread: [RESOLVED] Write XML using XmlDocument

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Resolved [RESOLVED] 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 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Write XML using XmlDocument

    Would it be too simple to just call the datatable's - or dataset's - .WriteXML property?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Write XML using XmlDocument

    Quote Originally Posted by dolot View Post
    Would it be too simple to just call the datatable's - or dataset's - .WriteXML property?
    That's how I've been doing it, the reason I don't want to do that this time is:
    (1) I've got multiple MasterNodes that'll fill the child nodes multiple times which makes it a bit more complicated than a single fill of the master and a single fill of each child then push it out to the xml file.
    (2) I'd like all of the child rows to be part of each master row so I don't need to filter the child tables when I pull the xml file back into the system. Basically for each master node, I'd like it to have all of it's child nodes right with it instead of all the children in their own table, separate from the master table.

    Here's my thought pattern pseudocode:
    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
    
    'Fill the child DataTables for the current master row in the loop
    
    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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Write XML using XmlDocument

    Looks like I need to fiddle with the XmlTextWriter: Create xml file Samples and examples - C#, VB.NET, ASP.NET
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Write XML using XmlDocument

    I'm such a lazoid...

    Rather than try to learn something new, I'd probably just build a de-normalized table with the master node columns and all 5 of the child node columns in one record, then populate the table with the master and child data and call the writexml method.

    I wouldn't consider that approach to be textbook perfect.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Write XML using XmlDocument

    Here's a sample from one of my projects
    Code:
    Dim claimDoc As New Xml.XmlDocument
    Dim rootElement As Xml.XmlElement = claimDoc.CreateElement("ROOT")
    claimDoc.AppendChild(rootElement)
    
    For Each row As DataRow In dt.Rows
    
        Dim claimId As String = row("claimid").ToString.TrimEnd
    
        Dim claimElement As Xml.XmlElement = claimDoc.CreateElement("Claim")
        rootElement.AppendChild(claimElement)
    
        Dim claimIdElement As Xml.XmlElement = claimDoc.CreateElement("claimid")
        claimElement.AppendChild(claimIdElement)
        claimIdElement.InnerText = claimId
    
    Next
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Write XML using XmlDocument

    That's funny, I had no problem writing XML but I couldn't figure out how to read it back properly

    I use the XmlTextWriter class, pretty easy.
    Here's an example (C#, but you should be able to convert easily) from a project I had lying around:
    csharp Code:
    1. public static void SaveMdiWindowLayout(Form form)
    2.         {
    3.             string path = GetMdiWindowLayoutFilePath();
    4.  
    5.             XmlTextWriter writer = new XmlTextWriter(path, null) { Formatting = Formatting.Indented };
    6.             writer.WriteStartDocument();
    7.             writer.WriteStartElement("MDIForms");
    8.  
    9.             foreach (PlaylistForm mdi in form.MdiChildren.Cast<PlaylistForm>())
    10.             {
    11.                 writer.WriteStartElement("Form");
    12.                 writer.WriteAttributeString("X", XmlConvert.ToString(mdi.Location.X));
    13.                 writer.WriteAttributeString("Y", XmlConvert.ToString(mdi.Location.Y));
    14.                 writer.WriteAttributeString("W", XmlConvert.ToString(mdi.Size.Width));
    15.                 writer.WriteAttributeString("H", XmlConvert.ToString(mdi.Size.Height));
    16.                 writer.WriteAttributeString("WS", mdi.WindowState.ToString());
    17.                 writer.WriteValue(mdi.FilePath);
    18.                 writer.WriteEndElement();
    19.             }
    20.  
    21.             writer.WriteEndElement();
    22.             writer.WriteEndDocument();
    23.             writer.Flush();
    24.         }

    Example output:
    xml Code:
    1. <?xml version="1.0"?>
    2. <MDIForms>
    3.   <Form X="50" Y="50" W="424" H="364" WS="Normal">C:\Users\Nick\Desktop\abc.plm</Form>
    4.   <Form X="213" Y="16" W="424" H="364" WS="Normal">C:\Users\Nick\Desktop\xyz.plm</Form>
    5. </MDIForms>

  8. #8

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Write XML using XmlDocument

    Thanks Nick, I'm fiddling with this today

    Though I am curious an a slightly related subject, why does VS flag this as a warning?
    Code:
    Dim writer As XmlTextWriter
    Try
        writer = New XmlTextWriter(FilePath, System.Text.Encoding.ASCII) With {.Formatting = Formatting.Indented}
    
        'write the opening of the document here
        'Write a bunch of data here
        'Write the closing of the document here
    Catch ex As Exception
        'Exception handling done here
    Finally
        If writer IsNot Nothing Then 
            writer.Flush()
            writer.Close()
        End If
    End Try
    Why does it flag writer as a null reference error could occur at run time? I'm checking for a null reference right there so no runtime errors could occur. I've only seen this a handful of times and it's always left me wondering.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9

  10. #10
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Write XML using XmlDocument

    I don't know why it does that either. If you were to write

    Dim writer As XmlTextWriter = Nothing

    The error would go away, even though you explicitly set it to nothing. So it seems the compiler doesn't differentiate very much. All it knows is when the object gets set - even if you set it to nothing, and in your finally block all it knows is that the object is nothing - even if that's what you're evaluating for.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  11. #11
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: Write XML using XmlDocument

    Opps! Meant to say...

    In your finally block, all the compiler knows is that the object wasn't set and that it might be nothing - even though that's what you're evaluating for.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

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