使用xmldocument c#的selectnodes

时间:2017-07-01 16:02:28

标签: c# xml

我的xml文件如下所示,我正在通过http读取 我有2个源挂载并使用xmldocument我可以选择源和解析 但是,选择“source”会返回两者。如何只选择mount =“/ AGMoffline2”

的源标签
<source mount="/AGMoffline">
    <listeners>0</listeners>
    <listenurl>http:/X.X.X.X/AGMoffline</listenurl>
    <max_listeners>30</max_listeners>
</source>
<source mount="/AGMoffline2">
    <listeners>0</listeners>
    <listenurl>http://X.X.X.X/AGMoffline</listenurl>
    <max_listeners>30</max_listeners>
</source>
XmlDocument tournamentsXML = new XmlDocument();
tournamentsXML.LoadXml(xml);
XmlNodeList nodes = tournamentsXML.DocumentElement.SelectNodes("source");

foreach (XmlNode node in nodes)
{
    //roll through node
}

3 个答案:

答案 0 :(得分:1)

使用Where过滤:

var tournamentsXML = XElement.Parse(xml);

foreach (var node in tournamentsXML.Elements("source").Where(el => (string)el.Attribute("mount") == "/AGMoffline2"))
    // use node

或者如果你只想要第一个:

var node = tournamentsXML.Elements("source").First(
    el => (string)el.Attribute("mount") == "/AGMoffline2");

答案 1 :(得分:1)

我创建了一个示例,您可以找到读取xml节点结构的逻辑。这是我项目的一部分。它不用担心。

Click here查看示例XML文档。

Bridge Class

public class DummyBook
{
    #region Properties
    private string _name;
    private string _author;
    private string _publishedDate;
    private string _publisher;
    private string _pageCount;
    private string _language;
    private string _additionalInfo;
    private string _coverFrontURL;
    private string _coverBackURL;
    private string _thumbURL;
    #endregion

    #region Getters and Setters
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string Author
    {
        get { return _author; }
        set { _author = value; }
    }

    public string PublishedDate
    {
        get { return _publishedDate; }
        set { _publishedDate = value; }
    }

    public string Publisher
    {
        get { return _publisher; }
        set { _publisher = value; }
    }

    public string PageCount
    {
        get { return _pageCount; }
        set { _pageCount = value; }
    }

    public string Language
    {
        get { return _language; }
        set { _language = value; }
    }

    public string AdditionalInfo
    {
        get { return _additionalInfo; }
        set { _additionalInfo = value; }
    }

    public string CoverFrontURL
    {
        get { return _coverFrontURL; }
        set { _coverFrontURL = value; }
    }

    public string CoverBackURL
    {
        get { return _coverBackURL; }
        set { _coverBackURL = value; }
    }

    public string ThumbURL
    {
        get { return _thumbURL; }
        set { _thumbURL = value; }
    }
    #endregion
}

XML反序列化函数

    public static DummyBook XML(string XMLPath)
    {
        DummyBook dummyBook = new DummyBook();
        XmlTextReader reader = new XmlTextReader(XMLPath);

        while (reader.Read())
            if (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.Name == "Title")
                    dummyBook.Name = reader.ReadString();
                else if (reader.Name == "Author")
                    dummyBook.Author += reader.ReadString() + ", ";
                else if (reader.Name == "PublicationDate")
                    dummyBook.PublishedDate = reader.ReadString();
                else if (reader.Name == "Publisher")
                    dummyBook.Publisher = reader.ReadString();
                else if (reader.Name == "NumberOfPages")
                    dummyBook.PageCount = reader.ReadString();
                else if (reader.Name == "Language")
                {
                    if (reader.ReadToDescendant("Name"))
                        dummyBook.Language = reader.ReadString();
                }
                else if (reader.Name == "Content")
                    dummyBook.AdditionalInfo = reader.ReadString();
                else if (reader.Name == "ImageSet")
                {
                    if (reader.GetAttribute("Category").ToString() == "primary")
                    {
                        if (reader.ReadToDescendant("LargeImage"))
                            if (reader.ReadToDescendant("URL"))
                                dummyBook.CoverFrontURL = reader.ReadString();
                    }
                    else if (reader.GetAttribute("Category").ToString() == "variant")
                    {
                        if (reader.ReadToDescendant("LargeImage"))
                            if (reader.ReadToDescendant("URL"))
                                dummyBook.CoverBackURL = reader.ReadString();
                    }
                }
            }
        reader.Close();
        return dummyBook;
    }

PS :XMLPath是XML文档文件的路径。

答案 2 :(得分:1)

  

由于我们正在处理XML文档,我们应该采用XML方式:)

SelectNodes和xpath表达式所以你应该使用XPath查询来选择所需的节点。

这是一个完整的工作源代码

police_publiclocation

希望它有所帮助!