0

I have an XML file which is similar to below. At the moment if I want to change values I have to go into the XML and change/add/remove records as required.

<configuration>
    <locations>
        <add key="1234" type="Type1" location="Default Location 1" value="10"/>
        <add key="4567" type="Type2" location="Default Location 1" value="13"/>
        <add key="7890" type="Type1" location="Default Location 2" value="17"/>
    </locations>
</configuration>

I'm writing a Windows Form GUI for this and a few other XMLs which the software uses. I can get/putsettings in the other XMLs as they have node names, but this file, (when originally created) was made differently.

I need to get each row as a string so I can then split it and display what I need on the screen (key/type/location/value). I then need to update the file with the information when updated.

I'm looking for some help in:

  • retrieving all node attributes within <locations>

  • clearing out all nodes within <locations> and then adding the nodes with attributes back into so that all eventualities are considered (records removed/added/updated) etc

har07
  • 88,338
  • 12
  • 84
  • 137
Paul Stringer
  • 90
  • 2
  • 10
  • Hi, these are the pages I've looked at so far, it may be that one of these has the answer but I've limited knowledge and still learning bits and pieces at the moment: http://stackoverflow.com/questions/2915294/iterating-through-all-nodes-in-xml-file http://stackoverflow.com/questions/11993857/read-all-xml-child-nodes-of-each-specific-node http://stackoverflow.com/questions/1600065/how-to-read-attribute-value-from-xmlnode-in-c http://www.csharp-examples.net/xml-nodes-by-attribute-value/ – Paul Stringer Sep 18 '14 at 09:38

3 Answers3

0

You could use an XmlReader to do the work for you.

Something like this;

        XmlReader reader = new XmlReader(filepath)

        string s = "";

        while(reader.Read())
        {
              if(reader.HasAttributes)
              {
               s  = reader["attributename"].Value;
              }
         }

Can't promise it will compile since I typed it from my phone.

Thereafter, you can use the stored values and use the XmlWriter to write the data to a file.

I'd also like to point out that if you are working with lots of data, XmlReader is probably the way to go. Using XmlDocument will load the entire document in the RAM, which could lead to performance issues. XmlReader will stream the data, using way less memory than XmlDocument will ever do.

Dion V.
  • 2,090
  • 2
  • 14
  • 24
  • i'll give this a try, thanks for the information about XMLReader too!! greatly appreciated – Paul Stringer Sep 18 '14 at 09:46
  • Since I didn't fully answer your question, I also added a link to XmlWriter which should help you write the data to a file. Glad I can help :) – Dion V. Sep 18 '14 at 09:49
0

I suggest you just use the XmlSerializer class in namespace System.Xml.Serialization. You can use attribute microsoft define. Then you can serialize and deserialize the XML to your structure or class easily.

Druid
  • 6,423
  • 4
  • 41
  • 56
alking
  • 1
0

Look into XDocument from System.Xml.Linq namespace. It is a newer API for dealing with XML document compared to the older XmlDocument class. And it is very easy to use in common scenario compared to XmlDocument or XmlReader. Usage example :

XDocument doc = XDocument.Load("path_to_xml_file.xml");
List<XElement> adds = doc.Descendants("locations").Elements("add");
foreach(XElement add in adds)
{
    //get attribute of current <add> node, for example key & type attribute :
    var key = (int)add.Attribute("key");
    var type = (string)add.Attribute("type");
    .....
}
har07
  • 88,338
  • 12
  • 84
  • 137