Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NETWinForm
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
<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 9-Nov-13 16:51pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.  
  3. 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[^].
  4.  
  5. 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
  Permalink  
v2
Comments
Zachary.shupp at 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 at 9-Nov-13 23:45pm
   
And what's the problem with that? Your idea is correct... —SA
Zachary.shupp at 10-Nov-13 9:41am
   
When I run it I get "Expression must evaluate to a node-set".
Zachary.shupp at 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 at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 375
1 Abhinav S 333
2 OriginalGriff 330
3 sanket saxena 305
4 CPallini 175
0 Sergey Alexandrovich Kryukov 8,497
1 OriginalGriff 4,850
2 Peter Leow 3,839
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 10 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid