从XML中读取特定项

时间:2016-01-23 04:53:29

标签: c# xml

让我们从我的目标开始吧。我需要阅读一个相当大的XML文件,并提取6个值。这些值位于子组中,我不知道如何访问。

这是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>

我在这里要做的是从Project-Subsystems-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"])
                {

                }
            }
        }

那里也是一样。

2 个答案:

答案 0 :(得分:1)

下次至少发布有效的xml。您将获取属性名称为Thing的所有值节点,然后从该元素获取子节点值。

SELECT "int", "type", "from" FROM "from" WHERE "int" = "type"

答案 1 :(得分:0)

使用XML序列化&amp;反序列化,使事情变得更容易

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