2012-01-27 27 views
0

XMLファイルのノードを読み込んで解析するコードを作成していますが、ファイルのレイアウトは複雑です(少なくとも私にはそう思われます。このプロジェクトまでの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 */ 

私の問題は、私はパース(あるいはしようとしていたときに発生します場合によっては依存関係のノードを解析するために使用されます)。私はそれらのそれぞれ(GroupNameとNameからAnd、そしてGroupNameとOrのOrから)をパースする方法について完全にはわからない。私は彼らが欲しいと思うように動作していないような行をコメントしました。

私がコメントアウトしたforeachループは、明らかにGeneral Node ListのすべてのDependenciesを読んでいます。そして "groupDepAnd ="(18行目)を始める行は、私にヌル参照例外を与えられません。

このXMLファイルを正しく解析しようとしていますか?私は間違って何をしていますか?すべての助けが大いに感謝されるでしょう。前にも述べたように、XMLに関する私の経験は非常に限られています。あなたが抱えている問題は、おそらくこれらの修正プログラムによって解決されている

答えて

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"); 

希望、それはまた、複数のそしてかがある場合はループの中で、あなたが何かをする必要があることは注目に値します、助け古い値を上書きしている瞬間です。

もう1つのことは、XmlDocumentの代わりに新しいXDocumentを使用することもできます。 Linq-esqueほど多くの人が好む人もいます。それは承認されたJohn Skeetです:

XDocument or XmlDocument

関連する問題