Howdy, guys.
I've been perusing this section a little here and there the last couple of days, and Google extensively, without finding a good example for what I need. I've decided I'd better actually ask. I believe the contrast between the solutions I see and my code is a minor misalignment of my perspective (xpath or something similar is scratching at the front door of my brain).
I'm modifying an XML document with a structure along these lines:
I want to add a new node, "myNode" with a parameter "myParameter" value "true", but I want it to fall in line here:
It seems like there should be an easy way to do it, but it's not jumping out at me. The following code works, but I don't control the XML document, so relying on indexes seems bad -- and I'm positive it's way more code than I need, plusalso blatant misuse of the concept of subroutines.
I'm leaving this rather vague because I'm hoping someone comfortable with the System.Xml object model can say, "oh, easy, just do these couple of lines of code," rather than debugging some minor syntax error or slightly improving my logic.
So, any thoughts?
I've been perusing this section a little here and there the last couple of days, and Google extensively, without finding a good example for what I need. I've decided I'd better actually ask. I believe the contrast between the solutions I see and my code is a minor misalignment of my perspective (xpath or something similar is scratching at the front door of my brain).
I'm modifying an XML document with a structure along these lines:
VB.NET:
< config>
<something par="true">
<other stuff="false" />
</something>
<!-- Below are important things-->
<impstuff>
<whynot paramstuff="whatever" />
<lilgroop>
<lilsetting lilpar="true" />
</lilgroop>
<fordrules rwd="true" />
</impstuff>
< /config>
I want to add a new node, "myNode" with a parameter "myParameter" value "true", but I want it to fall in line here:
VB.NET:
< config>
<something par="true">
<other stuff="false" />
</something>
<!-- Below are important things-->
<impstuff>
<whynot paramstuff="whatever" />
<[B]myNode myParameter="true" />[/B]
<lilgroop>
<lilsetting lilpar="true" />
</lilgroop>
<fordrules rwd="true" />
</impstuff>
< /config>
It seems like there should be an easy way to do it, but it's not jumping out at me. The following code works, but I don't control the XML document, so relying on indexes seems bad -- and I'm positive it's way more code than I need, plusalso blatant misuse of the concept of subroutines.
VB.NET:
Public Sub AddSetting(ByRef objXMLDoc As Xml.XmlDocument)
Try
' Create a new element node.
Dim objNode As XmlNode
Dim objAttribute As XmlAttribute
objAttribute = objXMLDoc.CreateAttribute(XmlNodeType.Attribute, "myParameter", "")
objAttribute.Value = "true"
objNode = objXMLDoc.CreateNode(XmlNodeType.Element, "myNode", "")
objNode.Attributes.Append(objAttribute)
objXMLDoc.ChildNodes.Item(0).ChildNodes.Item(2).AppendChild(objNode)
LogWrite("Display the modified XML document...")
LogWrite(objXMLDoc.OuterXml)
Catch ex As Exception
Debug.Print("AddSetting() Exception: " & _
" Exception.Message: " & ex.Message & vbCrLf & _
" Exception.HelpLink: " & ex.HelpLink & vbCrLf & _
" Exception.Source: " & ex.Source & vbCrLf & _
" Exception.StackTrace: " & ex.StackTrace & vbCrLf & _
" Exception.TargetSite: " & ex.TargetSite.ToString)
End Try
End Sub
I'm leaving this rather vague because I'm hoping someone comfortable with the System.Xml object model can say, "oh, easy, just do these couple of lines of code," rather than debugging some minor syntax error or slightly improving my logic.
So, any thoughts?