0
votes

My xml file looks like the below which I'm reading in via http I have 2 source mounts and using xmldocument I can select the source and parse However selecting "source" returns both. How can I select only the source tags where 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
Do you have two source tags at root or are they wrapped inside another tag? - jdweng
so they are actually wrapped inside another root tag - AShah

3 Answers

1
votes

Since we are dealing with XML document,we should it XML way :)

SelectNodes takes and xpath expression So You should be using XPath query to select the required nodes.

Here's a complete working source code

XmlDocument tournamentsXML = new XmlDocument();
            tournamentsXML.LoadXml(@"<root><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></root>");
            XmlNodeList nodes = tournamentsXML.DocumentElement.SelectNodes("//source[@mount='/AGMoffline2']");

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

Hope it helps!

1
votes

Filter with Where:

var tournamentsXML = XElement.Parse(xml);

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

Or if you just want the first:

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

I created an example that you can find the logic of the reading xml node structure. It's a part of my project. It works don't worry.

Click here to check out example XML Document.

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 Deserializer Function

    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 is the path of the XML document file.