Resolved Write XML using XmlDocument

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
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?
 
This is how I start one:
VB.NET:
 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:
This is how I start one it:
VB.NET:
 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:
VB.NET:
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.
 
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.

VB.NET:
<rootnode>.AppendChild(xmldoc.CreateElement("name"))

There probably is a better way, but this is what I know. Of course Datasets read/write xmls.
 
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).
 
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.
 
OK, here is an example, if you have something more specific let me know.

VB.NET:
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
 
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.)
 
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:
VB.NET:
<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>
 
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.
 
Back
Top