3

I'm using c# to get a parameters from xml file. My problem is I want to read only for the current program parameters. (v1.0, v1.1, v1.2... )

<?xml version="1.0" encoding="utf-8" ?>
<ApplicationPool>
    <Resource Version="V1.0">
        <Input>2000</Input>
        <Input>210</Input>
        <Input>65000</Input>
    </Resource>
    <Resource Version="V1.1">
        <Input>2500</Input>
        <Input>400</Input>
        <Input>130000</Input>
    </Resource>
</ApplicationPool>

using (XmlReader reader = XmlReader.Create("testXml.xml"))
{
    while (reader.Read())
    {
        if (reader.IsStartElement())
        {
            if (reader["Version"] == actualVersion)
            {
                //??
            }
        }
    }
}
1
  • 2
    Do you have any reason to use XmlReader here? I would strongly advise you to use LINQ to XML if you possibly can. I'd only use XmlReader either to read a potentially-enormous file without loading it into memory, or in order to implement an interface described in terms of XmlReader. Commented Nov 29, 2016 at 7:13

5 Answers 5

1
XDocument doc = XDocument.Load("testXml.xml")

var result = doc.Root.Descendants("Resource")
                      .Where(x => x.Attribute("Version")!= null 
                                   && x.Attribute("Version").Value == actualVersion);

This will return you all Resource nodes in which the Attribute Version == actualVersion. After that you can do whatever you want with the node.

Sign up to request clarification or add additional context in comments.

Comments

0
if (reader["Version"] == actualVersion)
{
    while (reader.ReadToFollowing("Input"))
    {
        string value = reader.ReadElementContentAsString();
        // or
        int value = reader.ReadElementContentAsInt();
    }
}

Comments

0

You can use the Xml Linq Approach like:

var xmlFile= XElement.Load(xmlString);
var actualVersion = "V1.1";
var requiredXmlData = xmlFile.Elements("Resource").Where(c=>c.Attribute("Version").Value==actualVersion );

Comments

0
using System.Xml;
...
string actualVersion="V1.1";
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load("testXML.xml");
XmlNodeList _ngroups = rssDoc.GetElementsByTagName("Resource");
for(int i=0;i<=_ngroups.Count-1;i++)
{
    if(_ngroups[i].Attributes[0].InnerText.ToString()==actualVersion)
    {
        for(int j=0;j<=_ngroups[i].ChildNodes.Count-1;j++)
              Console.WriteLine(_ngroups[i].ChildNodes[j].InnerText);
   }
}
...

Comments

0

Using combination of XmlReader and xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            while (!reader.EOF)
            {
                if (reader.Name != "Resource")
                {
                    reader.ReadToFollowing("Resource");
                }
                if (!reader.EOF)
                {
                    XElement resource = (XElement)XElement.ReadFrom(reader);
                    string version = (string)resource.Attribute("Version");
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.