解析“复杂”的XML文件

时间:2012-01-27 15:38:14

标签: xml c#-4.0

我正在研究一些代码来读取和解析XML文件的节点,并且文件的布局很复杂(​​至少,对我来说似乎是这样 - 在此项目之前还没有完成和解析XML)

xml文件的格式类似于:

<Parameters>
  <General>
    <Name>Name of this parameter</Name>
    <Caption>Information about the parameter</Caption>
    <GroupName>A Small Group of parameters</GroupName>
    <Value>1</Value>
    <Type>Boolean</Type>
    <Description>Some description</Description>
    <Dependencies>
      <And>
        <GroupName>Some other group of parameters</GroupName>
        <Name>The parameter this one is dependent on</Name>
      </And>
      <Or>
        <GroupName>Some other group of parameters</GroupName>
        <Name>The parameter this one might be dependent on</Name>
      </Or>
    </Dependencies>
  </General>
</Parameters>

我正在以这种方式解析文件:

XmlDocument xDoc = new XmlDocument();
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream s = _assembly.GetManifestResourceStream(@"NameOfAssembly.NameOfFile.xml");
xDoc.Load(s);
xmlTests = xDoc.SelectNodes("/Parameters/General");
foreach (XmlNode rootType in xmlTests)
{
    switch (rootType.Name.ToString())
    {
        case "General":
            name = rootType["Name"].InnerText;
            caption = rootType["Caption"].InnerText;
            groupName = rootType["GroupName"].InnerText;
            mandatory = rootType["Mandatory"].InnerText;
            value = Convert.ToInt32(rootType["Value"].InnerText);
            typeOfData = rootType["Type"].InnerText;
            description = rootType["Description"].InnerText;
            //groupDepAnd = rootType["Dependencies/And/GroupName"].InnerText;
            //XmlNodeList dependenciesList = rootType.SelectNodes("Parameters/General/Dependencies/And");
            //foreach (XmlNode dependenciesAndNode in dependenciesList)
            //{
                //groupDepAnd = dependenciesAndNode["GroupName"].InnerText;
                //namesDepAnd = dependenciesAndNode["Names"].InnerText;
            //}
            //dependenciesList = rootType.SelectNodes("Parameters/General/Dependencies/Or");
            //foreach (XmlNode dependenciesOrNode in dependenciesList)
            //{
                //groupDepOr = dependenciesOrNode["GroupName"].InnerText;
                //namesDepOr = dependenciesOrNode["Names"].InnerText;
            //}
            /* send values to object constructor */
            break;
        default:
            break;
    }
}
/*close Stream, xDoc and such */

当我正在解析(或尝试解析,视情况而定)依赖关系节点时,我的问题出现了。我不完全确定如何解析它们中的每一个(来自And的GroupName和Name,然后将Or中的GroupName和Name)解析为字符串。我已经注释掉了我想要它们似乎没有用的线条。

我已经注释掉的foreach循环,显然是通读了General Node List中所有Dependencies的提及。并且以“groupDepAnd =”(第18行)开头的行无法给出空引用异常。

我是否正在尝试正确解析此XML文件?我究竟做错了什么?任何和所有的帮助将不胜感激。正如我之前所说,我对XML的经验非常有限。

1 个答案:

答案 0 :(得分:1)

您遇到的问题可能会通过以下修复解决:

//this selects the value of just the first 'And' node, SelectSingleNode lets you use XPath
groupDepAnd = rootType.SelectSingleNode("Dependencies/And/GroupName").InnerText;  

//You were using the wrong XPath to get the collection, rootType is already at /Parameters/General
XmlNodeList dependenciesList = rootType.SelectNodes("Dependencies/And");

希望有所帮助,值得注意的是,在循环中,如果有多个And或Or节点,则需要执行某些操作,此时您只需覆盖旧值。

我要添加的另一件事是,您还可以使用较新的XDocument代替XmlDocument。有些人更喜欢它,因为它更像Linq-esque。这甚至是John Skeet的批准:

XDocument or XmlDocument