JMO9966
asked on
Appending an XMLDocument or use a Streamwriter??
Hello,
I have a DataTable I loop through and each row will need to have it's own section of "WorkCenterAddRq" in the following XML format. The following example is one Work Center row from my DataTable. The second row would need to start another <WorkCenterAddRq> in the location I note below.
Notice at the very bottom, I call the Save method for the XMLDocument. Once I do this is there a way to append this file with rows 2-999 where noted below or will I need to write all my XML to a string and then send to StreamWriter each time a new record is added??
Thanks,
JMO9966
Here's the XML data generated by the Save method at the end of my code below. I'd like to append this xml file for each row in my DataTable.
<JBXML>
<JBXMLRequest Session="13D3BDF9C9A64108A 0BF73ED162 BF1CC" />
<WorkCenterAddRq>
<WorkCenterAdd>
<ID>WCPress</ID>
</WorkCenterAdd>
<DepartmentRef>Machining</ Department Ref>
<Capacity>
<LimitingResources>2</Limi tingResour ces>
<SchedulingHours>8</Schedu lingHours>
<QueuHours>0</QueuHours>
</Capacity>
<BurdenCosts>
<LaborBurdenRate>10</Labor BurdenRate >
<MachineBurdenRate>50</Mac hineBurden Rate>
<GABurdenRate>15</GABurden Rate>
</BurdenCosts>
<LaborCosts>
<SetupLaborRate>40</SetupL aborRate>
<RunLaborRate>25</RunLabor Rate>
</LaborCosts>
</WorkCenterAddRq>
.......................... .......... .......... .......... .......... .......... .......... .WOULD LIKE THE SECOND ROW TO CREATE IT's <WorkCenterAddRq> SECTION BEGINNING HERE.
</JBXML>
I generate my xml by looping through DataTable like this:
For i = 0 To (dsSDK.Tables("tblWork_Cen ters").Row s.Count - 1)
.......get values
'create XML
If i = 0 Then
'Dim newNode As XmlNode = xmlRequestDoc.CreateNode
Dim newElem1 As XmlElement = xmlRequestDoc.CreateElemen t("JBXML")
xmlRequestDoc.AppendChild( newElem1)
'Dim newElem2 As XmlElement = xmlRequestDoc.CreateElemen t(strJBXML )
'xmlRequestDoc.AppendChild (newElem2)
Dim newElem2 As XmlElement = xmlRequestDoc.CreateElemen t("JBXMLRe quest")
newElem1.AppendChild(newEl em2)
'create attribute
Dim attrJBXMLRequest As XmlAttribute = xmlRequestDoc.CreateAttrib ute("Sessi on")
attrJBXMLRequest.Value = sessionID
'attrJBXMLRequest.Value = strDblQuote & sessionID & strDblQuote
'add the new node
newElem2.SetAttributeNode( attrJBXMLR equest)
End If
If WC <> "" Then
Dim newElem3 As XmlElement = xmlRequestDoc.CreateElemen t("WorkCen terAddRq")
newElem1.AppendChild(newEl em3)
Dim newElem4 As XmlElement = xmlRequestDoc.CreateElemen t("WorkCen terAdd")
newElem3.AppendChild(newEl em4)
Dim newElem5 As XmlElement = xmlRequestDoc.CreateElemen t("ID")
newElem5.InnerText = WC
newElem4.AppendChild(newEl em5)
If Dept <> "" Then
Dim newElem6 As XmlElement = xmlRequestDoc.CreateElemen t("Departm entRef")
newElem6.InnerText = Dept
newElem3.AppendChild(newEl em6)
End If
If LimitResource <> "" Or LimitResource <> "0" Then
Dim newElem7 As XmlElement = xmlRequestDoc.CreateElemen t("Capacit y")
'newElem7.InnerText = WC
newElem3.AppendChild(newEl em7)
Dim newElem8 As XmlElement = xmlRequestDoc.CreateElemen t("Limitin gResources ")
newElem8.InnerText = LimitResource.ToString
newElem7.AppendChild(newEl em8)
End If
'Save the document to a file and auto-indent the output
xmlRequestDoc.Save("VBNET. xml")
I have a DataTable I loop through and each row will need to have it's own section of "WorkCenterAddRq" in the following XML format. The following example is one Work Center row from my DataTable. The second row would need to start another <WorkCenterAddRq> in the location I note below.
Notice at the very bottom, I call the Save method for the XMLDocument. Once I do this is there a way to append this file with rows 2-999 where noted below or will I need to write all my XML to a string and then send to StreamWriter each time a new record is added??
Thanks,
JMO9966
Here's the XML data generated by the Save method at the end of my code below. I'd like to append this xml file for each row in my DataTable.
<JBXML>
<JBXMLRequest Session="13D3BDF9C9A64108A
<WorkCenterAddRq>
<WorkCenterAdd>
<ID>WCPress</ID>
</WorkCenterAdd>
<DepartmentRef>Machining</
<Capacity>
<LimitingResources>2</Limi
<SchedulingHours>8</Schedu
<QueuHours>0</QueuHours>
</Capacity>
<BurdenCosts>
<LaborBurdenRate>10</Labor
<MachineBurdenRate>50</Mac
<GABurdenRate>15</GABurden
</BurdenCosts>
<LaborCosts>
<SetupLaborRate>40</SetupL
<RunLaborRate>25</RunLabor
</LaborCosts>
</WorkCenterAddRq>
..........................
</JBXML>
I generate my xml by looping through DataTable like this:
For i = 0 To (dsSDK.Tables("tblWork_Cen
.......get values
'create XML
If i = 0 Then
'Dim newNode As XmlNode = xmlRequestDoc.CreateNode
Dim newElem1 As XmlElement = xmlRequestDoc.CreateElemen
xmlRequestDoc.AppendChild(
'Dim newElem2 As XmlElement = xmlRequestDoc.CreateElemen
'xmlRequestDoc.AppendChild
Dim newElem2 As XmlElement = xmlRequestDoc.CreateElemen
newElem1.AppendChild(newEl
'create attribute
Dim attrJBXMLRequest As XmlAttribute = xmlRequestDoc.CreateAttrib
attrJBXMLRequest.Value = sessionID
'attrJBXMLRequest.Value = strDblQuote & sessionID & strDblQuote
'add the new node
newElem2.SetAttributeNode(
End If
If WC <> "" Then
Dim newElem3 As XmlElement = xmlRequestDoc.CreateElemen
newElem1.AppendChild(newEl
Dim newElem4 As XmlElement = xmlRequestDoc.CreateElemen
newElem3.AppendChild(newEl
Dim newElem5 As XmlElement = xmlRequestDoc.CreateElemen
newElem5.InnerText = WC
newElem4.AppendChild(newEl
If Dept <> "" Then
Dim newElem6 As XmlElement = xmlRequestDoc.CreateElemen
newElem6.InnerText = Dept
newElem3.AppendChild(newEl
End If
If LimitResource <> "" Or LimitResource <> "0" Then
Dim newElem7 As XmlElement = xmlRequestDoc.CreateElemen
'newElem7.InnerText = WC
newElem3.AppendChild(newEl
Dim newElem8 As XmlElement = xmlRequestDoc.CreateElemen
newElem8.InnerText = LimitResource.ToString
newElem7.AppendChild(newEl
End If
'Save the document to a file and auto-indent the output
xmlRequestDoc.Save("VBNET.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The XmlDocument class handles your xml file as a tree ... i.e. handling of closing tags like </JBXM> is automatic
if you append the element in its correct place every thing is fine ... What GetTag function does is execute a xpath query on the xmldocument it's like a database query but for xml it only returns the parent node that you want to append child nodes to ... if yuo have any other questions just ask.. :)
This article offers great explanation on xpath queries and more complicated queries than the one I presented you with. :)
http://www.developer.com/xml/article.php/3383961
And don't forget to ask when you need anything :) ...
if you append the element in its correct place every thing is fine ... What GetTag function does is execute a xpath query on the xmldocument it's like a database query but for xml it only returns the parent node that you want to append child nodes to ... if yuo have any other questions just ask.. :)
This article offers great explanation on xpath queries and more complicated queries than the one I presented you with. :)
http://www.developer.com/xml/article.php/3383961
And don't forget to ask when you need anything :) ...
ASKER
I thought I was done but one last question, I will open another case if needed.
My final output actually has changed slightly. I need to start the second WorkCenterAddRq in the line noted below. I now have both </JBXMLRequest> and </JBXML> that will always be the last two lines of my xml document. Previously, I cited only </JBXML> in our last converstion.
I have this code in the AppendNewChild Sub you recommended.
This code you mentioned works good, but I now have to append to a different location in the xml file:
xmlRequestDoc.Load("VBNET. xml")
Dim Element1 as Xml.XmlElement = GetTag("/JBXML")
Dim newElem3 as XmlElement = xmlRequestDoc.CreateElemen t("WorkCen terAddRq")
Element1.AppendChild(newEl em3)
I'm confused at how appending to tag </JBXML> placed this <WorkCenterAddRq> the line before </JBXML> rather than after but it did work and was what I wanted.
I tried changing AppendNewChild sub to this but it errors on "NullReferenceException" ?? Can't figure out why sub runs fine for one Element ("/JBXML") but not another ("/JBXMLRequest")
xmlRequestDoc.Load("VBNET. xml")
Dim Element1 as Xml.XmlElement = GetTag("/JBXMLRequest")
Dim newElem3 as XmlElement = xmlRequestDoc.CreateElemen t("WorkCen terAddRq")
Element1.AppendChild(newEl em3) - this is the line that gets "NullReferenceException" error
<JBXML>
<JBXMLRequest Session="13D3BDF9C9A64108A 0BF73ED162 BF1CC">
<WorkCenterAddRq>
<WorkCenterAdd>
<ID>WCPress</ID>
</WorkCenterAdd>
<DepartmentRef>Machining</ Department Ref>
<Capacity>
<LimitingResources>2</Limi tingResour ces>
<SchedulingHours>8</Schedu lingHours>
<QueuHours>0</QueuHours>
</Capacity>
<BurdenCosts>
<LaborBurdenRate>10</Labor BurdenRate >
<MachineBurdenRate>50</Mac hineBurden Rate>
<GABurdenRate>15</GABurden Rate>
</BurdenCosts>
<LaborCosts>
<SetupLaborRate>40</SetupL aborRate>
<RunLaborRate>25</RunLabor Rate>
</LaborCosts>
</WorkCenterAddRq>
.......................... .......... .......... .......... .......... .......... .......... .WOULD LIKE THE SECOND ROW TO CREATE IT's <WorkCenterAddRq> SECTION BEGINNING HERE.
</JBXMLRequest>
</JBXML>
Thanks Mohzedan!!
My final output actually has changed slightly. I need to start the second WorkCenterAddRq in the line noted below. I now have both </JBXMLRequest> and </JBXML> that will always be the last two lines of my xml document. Previously, I cited only </JBXML> in our last converstion.
I have this code in the AppendNewChild Sub you recommended.
This code you mentioned works good, but I now have to append to a different location in the xml file:
xmlRequestDoc.Load("VBNET.
Dim Element1 as Xml.XmlElement = GetTag("/JBXML")
Dim newElem3 as XmlElement = xmlRequestDoc.CreateElemen
Element1.AppendChild(newEl
I'm confused at how appending to tag </JBXML> placed this <WorkCenterAddRq> the line before </JBXML> rather than after but it did work and was what I wanted.
I tried changing AppendNewChild sub to this but it errors on "NullReferenceException" ?? Can't figure out why sub runs fine for one Element ("/JBXML") but not another ("/JBXMLRequest")
xmlRequestDoc.Load("VBNET.
Dim Element1 as Xml.XmlElement = GetTag("/JBXMLRequest")
Dim newElem3 as XmlElement = xmlRequestDoc.CreateElemen
Element1.AppendChild(newEl
<JBXML>
<JBXMLRequest Session="13D3BDF9C9A64108A
<WorkCenterAddRq>
<WorkCenterAdd>
<ID>WCPress</ID>
</WorkCenterAdd>
<DepartmentRef>Machining</
<Capacity>
<LimitingResources>2</Limi
<SchedulingHours>8</Schedu
<QueuHours>0</QueuHours>
</Capacity>
<BurdenCosts>
<LaborBurdenRate>10</Labor
<MachineBurdenRate>50</Mac
<GABurdenRate>15</GABurden
</BurdenCosts>
<LaborCosts>
<SetupLaborRate>40</SetupL
<RunLaborRate>25</RunLabor
</LaborCosts>
</WorkCenterAddRq>
..........................
</JBXMLRequest>
</JBXML>
Thanks Mohzedan!!
If you want the <WorkCenterAddRq> tag to be added before </JBXMLRequest> I want you to understand something
my GetTag Function executes something called xPath query this is a language similar in concept to SQL query but is used to query information from an XML file.... the parameter passed to the GetTag function is the Xpath Query ...for example... :
let's say we have the XML file like so:
<MainNode>
<SubNode1 id="0"/>
<SubNode1 id="1"/>
<SubNode2>
<SubNode21/>
</SubNode2>
</MainNode>
the xpath query to get all the "subnode1" tags is like this = "/MainNode/SubNode1"
the xpath query to get the "subnode2" tag is like this = "/MainNode/SubNode2"
the xpath query to get the "subnode21" tag is like this = "/MainNode/SubNode2/SubNod e21"
the xpath query to get the "subnode1" tag with ID attribute ="1" is like this = "/MainNode/SubNode1[@id="1 "]"
After that these xpath queries return to you either a single Element or a nodelist (element array)
when you want to add a child element to the end of the Current element child nodes you use
Element1.AppendChild()
if you want to add a child element to the beginning of the selected element child nodes you use
Element1.PrependChild()
in your case you would use this xpath query (I mentioned it as a comment in the first post :) ) ....
"/JBXML/JBXMLRequest[@Sess ion='" & SessionID & "']"
Dim Element1 as Xml.XmlElement = GetTag("/JBXML/JBXMLReques t[@Session ='" & SessionID & "']")
Dim newElem3 as XmlElement = xmlRequestDoc.CreateElemen t("WorkCen terAddRq")
Element1.AppendChild(newEl em3)
you have to supply the sessionId because I guess that your xml file will grow and include more sessions than this one
Anything vague just ask and I will answer your every question :) ... I love to help :)
my GetTag Function executes something called xPath query this is a language similar in concept to SQL query but is used to query information from an XML file.... the parameter passed to the GetTag function is the Xpath Query ...for example... :
let's say we have the XML file like so:
<MainNode>
<SubNode1 id="0"/>
<SubNode1 id="1"/>
<SubNode2>
<SubNode21/>
</SubNode2>
</MainNode>
the xpath query to get all the "subnode1" tags is like this = "/MainNode/SubNode1"
the xpath query to get the "subnode2" tag is like this = "/MainNode/SubNode2"
the xpath query to get the "subnode21" tag is like this = "/MainNode/SubNode2/SubNod
the xpath query to get the "subnode1" tag with ID attribute ="1" is like this = "/MainNode/SubNode1[@id="1
After that these xpath queries return to you either a single Element or a nodelist (element array)
when you want to add a child element to the end of the Current element child nodes you use
Element1.AppendChild()
if you want to add a child element to the beginning of the selected element child nodes you use
Element1.PrependChild()
in your case you would use this xpath query (I mentioned it as a comment in the first post :) ) ....
"/JBXML/JBXMLRequest[@Sess
Dim Element1 as Xml.XmlElement = GetTag("/JBXML/JBXMLReques
Dim newElem3 as XmlElement = xmlRequestDoc.CreateElemen
Element1.AppendChild(newEl
you have to supply the sessionId because I guess that your xml file will grow and include more sessions than this one
Anything vague just ask and I will answer your every question :) ... I love to help :)
ASKER
Thank You!!!
Most of my programs will only have one sessionID so I used this, but I will keep your parameter in mind for multiple session ID's also.
GetTag("/JBXML/JBXMLReques t") and it did exactly what I wanted.
You gave me the most well explained answers I've ever received on this site and that's saying a lot!
Thanks Mohzedan,
JMO9966
Most of my programs will only have one sessionID so I used this, but I will keep your parameter in mind for multiple session ID's also.
GetTag("/JBXML/JBXMLReques
You gave me the most well explained answers I've ever received on this site and that's saying a lot!
Thanks Mohzedan,
JMO9966
Your welcome .. if you need anything elese just ask . :)
ASKER
Thanks mohzedan.
Here's what I ended up with:
If row 1 ' generate "header" tags and first row data elements
dr = dsSDK.Tables("tblWorkCente
Element = xmlRequestDoc.CreateElment
xmlRequestDoc.AppendChild(
Dim newElem2 As XmlElement = xmlRequestDoc.CreateElemen
Element.AppendChild(newEle
'create attribute
Dim attrJBXMLRequest As XmlAttribute = xmlRequestDoc.CreateAttrib
attrJBXMLRequest.Value = sessionID
newElem2.SetAttributeNode(
'add data elements
If WC <> "" Then
Dim newElem3 As XmlElement = xmlRequestDoc.CreateElemen
newElem1.AppendChild(newEl
Dim newElem4 As XmlElement = xmlRequestDoc.CreateElemen
newElem3.AppendChild(newEl
Dim newElem5 As XmlElement = xmlRequestDoc.CreateElemen
newElem5.InnerText = WC
newElem4.AppendChild(newEl
If Dept <> "" Then
Dim newElem6 As XmlElement = xmlRequestDoc.CreateElemen
newElem6.InnerText = Dept
newElem3.AppendChild(newEl
End If
.......................
.......................
.......................
End If ' for Row 1
If Row2 or greater Then
AppendNewChild(dr)
End If
My XML file now looks like I want!
That getTag function grabs the last element in my xml file named </JBXML> and then when I called Save the second time it knows to add before this tag?? Is that correct?
Thanks Again,
JMO9966