|
-
Mar 31st, 2010, 08:40 AM
#1
[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?
-
Mar 31st, 2010, 09:44 AM
#2
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
-
Mar 31st, 2010, 09:49 AM
#3
Re: Write XML using XmlDocument
 Originally Posted by dolot
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
-
Mar 31st, 2010, 09:53 AM
#4
Re: Write XML using XmlDocument
-
Mar 31st, 2010, 03:22 PM
#5
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
-
Mar 31st, 2010, 04:05 PM
#6
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
-
Mar 31st, 2010, 04:34 PM
#7
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:
public static void SaveMdiWindowLayout(Form form) { string path = GetMdiWindowLayoutFilePath(); XmlTextWriter writer = new XmlTextWriter(path, null) { Formatting = Formatting.Indented }; writer.WriteStartDocument(); writer.WriteStartElement("MDIForms"); foreach (PlaylistForm mdi in form.MdiChildren.Cast<PlaylistForm>()) { writer.WriteStartElement("Form"); writer.WriteAttributeString("X", XmlConvert.ToString(mdi.Location.X)); writer.WriteAttributeString("Y", XmlConvert.ToString(mdi.Location.Y)); writer.WriteAttributeString("W", XmlConvert.ToString(mdi.Size.Width)); writer.WriteAttributeString("H", XmlConvert.ToString(mdi.Size.Height)); writer.WriteAttributeString("WS", mdi.WindowState.ToString()); writer.WriteValue(mdi.FilePath); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); }
Example output:
xml Code:
<?xml version="1.0"?> <MDIForms> <Form X="50" Y="50" W="424" H="364" WS="Normal">C:\Users\Nick\Desktop\abc.plm</Form> <Form X="213" Y="16" W="424" H="364" WS="Normal">C:\Users\Nick\Desktop\xyz.plm</Form> </MDIForms>
-
Apr 5th, 2010, 08:45 AM
#8
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.
-
Apr 5th, 2010, 09:11 AM
#9
Re: Write XML using XmlDocument
I think it's kind of a bug. If you initialize writer as Nothing then it stops the warning.
-
Apr 5th, 2010, 09:13 AM
#10
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
-
Apr 5th, 2010, 09:14 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|