Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have been learning how to work with XML files in vb.net and Im having issues appending new data to an existing xml file.

XML Layout
XML
<HelpBox>
  <Categorys>
    <Category Selected="True" Name="Admin Tools"></Category>
    <Category Selected="False" Name="Scripts"></Category>
    <Category Selected="False" Name="Clipboard"></Category>
  </Categorys>
  <Actions>
    <Item Name="RDC" Icon="1" TaskType="Launch" Category="Admin Tools">
      <File>C:\Windows\System32\mstsc.exe</File>
      <Arg />
    </Item>
    <Item Name="MSRA" Icon="2" TaskType="Launch" Category="Admin Tools">
      <File>C:\Windows\System32\msra.exe</File>
      <Arg>offerra</Arg>
    </Item>
  </Actions>
</HelpBox>


Now the issues I'm having are appending new items under Categorys and Actions. If someone could provide an example that would really help out. I have been searching google for the last 2 days and I haven't found and example that clearly shows how to append.
Posted

1 solution

First of all, the problem has nothing to do with Forms, or any other kind of UI. Now, appending any text to some valid XML is impossible, in the following sense: if you do that, the text is transformed to the text which cannot be, according the XML standard, a well-formed XML. This is because XML can have only one root element.

To obtain some well-formed XML, you can only add/insert some XML as a child(red) to existing XML element. To manipulate XML, .NET FCL offers different approaches. Here is my short overview of them:
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Good luck,
—SA
 
Share this answer
 
v2
Comments
Zachary.shupp 9-Nov-13 23:34pm    
I have been looking through System.Xml.XmlDocument and I see that I have to load it to memory and then modify it then save it back to the file. below is the function Im trying to get working and I still need a little help.

Dim nLine As String = Environment.NewLine
Dim newTool As String = I cant get my string to save in this comment
Try
' Load the XmlDocument.
Dim xd As New XmlDocument()
xd.Load(sPath)

Dim docFrag As XmlDocumentFragment = xd.CreateDocumentFragment()
docFrag.InnerXml = newTool
Dim root As XmlNode = xd.SelectSingleNode("/AdminLaunch/Categories/")
root.AppendChild(docFrag)

xd.Save(sPath)
Return True
Catch ex As Exception

MsgBox(ex.Message)
End Try
Sergey Alexandrovich Kryukov 9-Nov-13 23:45pm    
And what's the problem with that? Your idea is correct...
—SA
Zachary.shupp 10-Nov-13 9:41am    
When I run it I get "Expression must evaluate to a node-set".
Zachary.shupp 10-Nov-13 10:58am    
Okay so I fixed that error by modifying the code a little. the issue Im having now is the way its writing the category, it puts selected attribute before the Name attribute. Is there something im doing wrong in the code.

Dim xmlSettings As New XmlDocument
xmlSettings.Load(sPath)

Dim root As XmlNode = xmlSettings.SelectSingleNode("/AdminLaunch/Categories")
Dim xNode As XmlNode = xmlSettings.CreateElement("Category")
Dim xName As XmlAttribute = xmlSettings.CreateAttribute("Name")
Dim xSelected As XmlAttribute = xmlSettings.CreateAttribute("Selected")

xName.Value = "Scripts"
xSelected.Value = "True"

xNode.Attributes.Append(xName)
xNode.Attributes.Append(xSelected)

root.AppendChild(xNode)

xmlSettings.Save(sPath)
Sergey Alexandrovich Kryukov 10-Nov-13 11:51am    
Great.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900