用c解析复杂的xml文件

本文关键字:xml 文件 复杂 | 更新日期: 2023-09-27 18:20:57

我有一个xml文件,其结构如下:

<?xml version="1.0" encoding="UTF-8"?>
<EM>
   <DEFAULT>
      <Channel1>
         <CONFIG_PARAM>
            <Item name="line" value="0" />
            <Item name="ULimt" value="0" />
            <Item name="LLimit" value="0" />
            <Item name="CLabel" value="0" />
         </CONFIG_PARAM>
      </Channel1>
      <Channel2>
         <CONFIG_PARAM>
            <Item name="line" value="0" />
            <Item name="ULimt" value="0" />
            <Item name="LLimit" value="0" />
            <Item name="CLabel" value="1" />
         </CONFIG_PARAM>
      </Channel2>
      <CONFIG_PARAM>
         <Item name="Frequency" value="50" />
         <Item name="EMark" value="2" />
         <Item name="Lang" value="LANG_ENG" />
         <Item name="ID" value="111111111111111111111111111" />
         <Item name="AMethod" value="AUC_CM_ERROR" />
         <Item name="AValue" value="0" />
      </CONFIG_PARAM>
   </DEFAULT>
   <UPDATED>
      <Channel1>
         <CONFIG_PARAM>
            <Item name="line" value="0" />
            <Item name="ULimt" value="0" />
            <Item name="LLimit" value="0" />
            <Item name="CLabel" value="0" />
         </CONFIG_PARAM>
      </Channel1>
      <Channel2>
         <CONFIG_PARAM>
            <Item name="line" value="0" />
            <Item name="ULimt" value="0" />
            <Item name="LLimit" value="0" />
            <Item name="CLabel" value="1" />
         </CONFIG_PARAM>
      </Channel2>
      <CONFIG_PARAM>
         <Item name="Frequency" value="50" />
         <Item name="EMark" value="2" />
         <Item name="Lang" value="LANG_ENG" />
         <Item name="ID" value="111111111111111111111111111" />
         <Item name="AMethod" value="AUC_CM_ERROR" />
         <Item name="AValue" value="0" />
      </CONFIG_PARAM>
   </UPDATED>
</EM>

我有DEFAULT和UPDATED两个标签。在里面我有channel1、channel2和CONFIG_PARAM标签。我的要求是这样的:

  1. 在DEFAULT标记中,所有值都是默认值。和UPDATED标签在用户执行某些操作时具有最新更新的值

我想创建这样的属性:

default.Channel1
default.Channl2
default.Config_param

更新标签的方式相同:

updated.Channel1
updated.Channel2
updated.config_param 

例如,像这样,我想创建proprty:

private List<ParameterNode> channel1 = new List<ParameterNode>();
public List<ParameterNode> Channel1
{
    get
    {
        return this.channel1;
    }
}

请告诉我如何创建房产。

p.S:我写了这个代码来获得值:

// find the channel1 element and load the Item
XmlElement channel1Element = GetUniqueElement(doc.DocumentElement, "Channel1");
if (channel1Element != null)
    this.channel1 = ConfigParameterNode.LoadItems(channel1Element);
private static XmlElement GetUniqueElement(XmlNode parent, string name)
{
    XmlElement result = null;
    foreach (XmlNode node in parent.ChildNodes)
    {
    XmlElement element = node as XmlElement;
    if (element == null)
        continue;
    if (element.Name != name)
        continue;
    result = element;
    }
    return result;
}
internal static List<ParameterNode> LoadItems(XmlElement parent)
{
    List<ParameterNode> result = new List<ParameterNode>();
    // go through all the children and find the Item elements
    foreach (XmlNode child in parent.ChildNodes)
    {
        if (child.HasChildNodes)
        {
            foreach (XmlNode node in child.ChildNodes)
            {
                // is it an element
                XmlElement element = node as XmlElement;
                if (element == null)
                    continue;

                // yes, so add it if it's an Item element
                if (element.LocalName == "Item")
                    result.Add(new ParameterNode(element));
            }
        }
        else
        {
            // is it an element
            XmlElement element = child as XmlElement;
            if (element == null)
                continue;

            // yes, so add it if it's an Item element
            if (element.LocalName == "Item")
                result.Add(new ParameterNode(element));
        }
    }
    return result;
}

请告诉我如何更改代码,以便创建属性默认值。Channel1,默认值。Channel2和default.config_param.

用c解析复杂的xml文件

我得到了这个问题的答案。

XmlElement defElement=GetUniqueElement(doc.DocumentElement,"DEFAULT");

然后将这个defElement传递给其他方法。我将创建属性作为默认参数。