2016-01-23 70 views
2

讓我們從我的目標開始。我需要通讀一個相當大的XML文件,並提取6個值。這些值位於我不知道如何訪問的子組中的行中。從XML讀取特定項目


下面是XML結構:

<Project Guid="9e9a67f8" Name="GameThings" Version="1.7"> 
    <Subsystems> 
    <Values Name="Thing1"> 
     <Values Name="Stuff1" Type="int" Value="0" /> 
     <Values Name="Stuff2" Type="int" Value="5" /> 
    </Values> 
    <Values Name="Thing2"> 
     <Values Name="Top" Type="int" Value="10" /> 
     <Values Name="Bottom" Type="int" Value="15" /> 
    </Values> 
    </Subsystems> 
    <OtherSystems> 
    <Values Name="Thing3"> 
     <Values Name="Stuff3" Type="int" Value="0" /> 
     <Values Name="Stuff4" Type="int" Value="5" /> 
    </Values> 
    </OtherSystems> 
</Project> 

我想在這裏做的是檢索項目的子系統 - Thing2值。這就是我需要的。我該怎麼做?


這裏是我試過的事情之一:

 using (XmlTextReader reader = new XmlTextReader(Path + "Project.xml")) 
     { 
      while (reader.Read()) 
      { 
       if(reader.) 
      } 
     } 

但它基本上停在那裏。這裏是另外一件事,我再沒感,它主要是複製過去和我的直覺:

 XmlDocument xml = new XmlDocument(); 
     xml.LoadXml(Path + "Project.xml"); 
     XmlNodeList nodes = xml.SelectNodes("/Project[@*]/Subsystems"); 
     foreach(XmlNode node in nodes) 
     { 
      if(node["Name"].Value == "GameInfo") 
      { 
       foreach(XmlNode stuff in node["Name"]) 
       { 

       } 
      } 
     } 

同樣存在的東西。

+1

一堆的XPath例子:h ttps://msdn.microsoft.com/en-us/library/ms256086(v = vs.110).aspx –

+0

'我需要通讀一個相當大的XML文件,並提取6個值。'+'我' m試圖在這裏執行的是從Project-Subsystems-Thing2中檢索值。「Thing2」只有2個子節點。你需要檢索哪些'6'值,以及從哪裏獲取? – dxiv

+0

那些最內層的「值」元素似乎沒有被正確關閉。他們不應該關閉斜線嗎?也就是'... Value =「5」/>' –

回答

1

下一次atleast post xml有效。您將獲取屬性名稱爲Thing的所有值節點,然後從此元素獲取子節點值。

private static void XDocumentLINQ() 
     { 
      string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> 
<Project Guid=""9e9a67f8"" Name=""GameThings"" Version=""1.7""> 
    <Subsystems> 
    <Values Name=""Thing1""> 
     <Values Name=""Stuff1"" Type=""int"" Value=""0"" /> 
     <Values Name=""Stuff2"" Type=""int"" Value=""5"" /> 
    </Values> 
    <Values Name=""Thing2""> 
     <Values Name=""Top"" Type=""int"" Value=""10"" /> 
     <Values Name=""Bottom"" Type=""int"" Value=""15"" /> 
    </Values> 
    </Subsystems> 
    <OtherSystems> 
    <Values Name=""Thing3""> 
     <Values Name=""Stuff3"" Type=""int"" Value=""0"" /> 
     <Values Name=""Stuff4"" Type=""int"" Value=""5"" /> 
    </Values> 
    </OtherSystems> 
</Project>"; 

      XDocument doc = XDocument.Parse(xml); 

      var elements = doc.Descendants("Project").Elements("Subsystems").Elements("Values").Where(x=>x.Attribute("Name").Value == "Thing2").Elements("Values"); 

      foreach (XElement elem in elements) 
      { 
       Console.WriteLine(elem.Attribute("Name").Value + " value is: " + elem.Attribute("Value").Value); 
      } 

      Console.ReadKey(); 

     } 
+0

它不工作,list.Count是0,並且所有名稱都是完全正確的。 –

+0

如果你把控制檯應用程序,你會看到它正在工作。 – mybirthname

+0

在此之前,我沒有使用Linq,那麼解析XDocument文本的另一種方法是什麼?我目前正在做負載(路徑),我懷疑這就是爲什麼它不工作。 –

0

使用XML序列化&反序列化,這使得事情更加容易

類文件的XML

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class Project 
{ 

    private ProjectValues[] subsystemsField; 

    private ProjectOtherSystems otherSystemsField; 

    private string guidField; 

    private string nameField; 

    private decimal versionField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlArrayItemAttribute("Values", IsNullable = false)] 
    public ProjectValues[] Subsystems 
    { 
     get 
     { 
      return this.subsystemsField; 
     } 
     set 
     { 
      this.subsystemsField = value; 
     } 
    } 

    /// <remarks/> 
    public ProjectOtherSystems OtherSystems 
    { 
     get 
     { 
      return this.otherSystemsField; 
     } 
     set 
     { 
      this.otherSystemsField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Guid 
    { 
     get 
     { 
      return this.guidField; 
     } 
     set 
     { 
      this.guidField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public decimal Version 
    { 
     get 
     { 
      return this.versionField; 
     } 
     set 
     { 
      this.versionField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ProjectValues 
{ 

    private ProjectValuesValues[] valuesField; 

    private string nameField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Values")] 
    public ProjectValuesValues[] Values 
    { 
     get 
     { 
      return this.valuesField; 
     } 
     set 
     { 
      this.valuesField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ProjectValuesValues 
{ 

    private string nameField; 

    private string typeField; 

    private byte valueField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Type 
    { 
     get 
     { 
      return this.typeField; 
     } 
     set 
     { 
      this.typeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public byte Value 
    { 
     get 
     { 
      return this.valueField; 
     } 
     set 
     { 
      this.valueField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ProjectOtherSystems 
{ 

    private ProjectOtherSystemsValues valuesField; 

    /// <remarks/> 
    public ProjectOtherSystemsValues Values 
    { 
     get 
     { 
      return this.valuesField; 
     } 
     set 
     { 
      this.valuesField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ProjectOtherSystemsValues 
{ 

    private ProjectOtherSystemsValuesValues[] valuesField; 

    private string nameField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Values")] 
    public ProjectOtherSystemsValuesValues[] Values 
    { 
     get 
     { 
      return this.valuesField; 
     } 
     set 
     { 
      this.valuesField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
public partial class ProjectOtherSystemsValuesValues 
{ 

    private string nameField; 

    private string typeField; 

    private byte valueField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get 
     { 
      return this.nameField; 
     } 
     set 
     { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Type 
    { 
     get 
     { 
      return this.typeField; 
     } 
     set 
     { 
      this.typeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public byte Value 
    { 
     get 
     { 
      return this.valueField; 
     } 
     set 
     { 
      this.valueField = value; 
     } 
    } 
} 

代碼:

XmlSerializer deserializer = new XmlSerializer(typeof(Project)); 
     StreamReader reader = new StreamReader(xml); 
     object obj = deserializer.Deserialize(reader); 
     Project XmlData = (Project)obj; 
     reader.Close(); 
     var val = XmlData.Subsystems.ToList().Where(x=>x.Values.ToList().Select(y=>y.Name).Contains("Thing2")).ToList(); 
+1

如果您需要從XML中讀取大量不同的信息,但是如果您需要從複雜的XML中只取幾個節點,那麼這是一個好方法,那麼使用XmlDocument或XDocument –