Click here to Skip to main content
13,552,501 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have been learning how to work with XML files in and Im having issues appending new data to an existing xml file.

XML Layout
    <Category Selected="True" Name="Admin Tools"></Category>
    <Category Selected="False" Name="Scripts"></Category>
    <Category Selected="False" Name="Clipboard"></Category>
    <Item Name="RDC" Icon="1" TaskType="Launch" Category="Admin Tools">
      <Arg />
    <Item Name="MSRA" Icon="2" TaskType="Launch" Category="Admin Tools">

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
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.
  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.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.

Good luck,
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
' Load the XmlDocument.
Dim xd As New XmlDocument()

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

Return True
Catch ex As Exception

End Try
SAKryukov 9-Nov-13 23:45pm
And what's the problem with that? Your idea is correct...
Shupp 10-Nov-13 9:41am
When I run it I get "Expression must evaluate to a node-set".
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

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"



SAKryukov 10-Nov-13 11:51am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01-2016 | 2.8.180515.1 | Last Updated 10 Nov 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100