简体   繁体   English

读取XML文件以获取所有子节点

[英]Reading XML File to get all Child Nodes

I have an XML file which is similar to below. 我有一个类似于以下内容的XML文件。 At the moment if I want to change values I have to go into the XML and change/add/remove records as required. 目前,如果我想更改值,则必须进入XML并根据需要更改/添加/删除记录。

<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. 我正在为此和该软件使用的其他一些XML编写Windows Form GUI。 I can get/putsettings in the other XMLs as they have node names, but this file, (when originally created) was made differently. 我可以在其他XML中获取/设置,因为它们具有节点名,但是此文件(最初创建时)的制作方式有所不同。

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> 检索<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 清除<locations>所有节点,然后将具有属性的节点添加回去,以便考虑所有可能情况(删除/添加/更新记录)等

You could use an XmlReader to do the work for you. 您可以使用XmlReader为您完成工作。

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. 之后,您可以使用存储的值并使用XmlWriter将数据写入文件。

I'd also like to point out that if you are working with lots of data, XmlReader is probably the way to go. 我还想指出,如果您要处理大量数据,则XmlReader可能是XmlReader的方法。 Using XmlDocument will load the entire document in the RAM, which could lead to performance issues. 使用XmlDocument会将整个文档加载到RAM中,这可能会导致性能问题。 XmlReader will stream the data, using way less memory than XmlDocument will ever do. XmlReader将使用比XmlDocument更少的内存来流式传输数据。

I suggest you just use the XmlSerializer class in namespace System.Xml.Serialization . 我建议您只在名称空间System.Xml.Serialization使用XmlSerializer类。 You can use attribute microsoft define. 您可以使用Microsoft定义的属性。 Then you can serialize and deserialize the XML to your structure or class easily. 然后,您可以轻松地将XML序列化和反序列化为您的结构或类。

Look into XDocument from System.Xml.Linq namespace. System.Xml.Linq命名空间查看XDocument It is a newer API for dealing with XML document compared to the older XmlDocument class. 与较早的XmlDocument类相比,它是用于处理XML文档的较新API。 And it is very easy to use in common scenario compared to XmlDocument or XmlReader . XmlDocumentXmlReader相比,在常见情况下使用起来非常容易。 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");
    .....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM