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)
            {
                //??
            }
        }
    }
}
Yael
  • 1,566
  • 3
  • 18
  • 25
Smith
  • 123
  • 1
  • 11
  • 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`. – Jon Skeet Nov 29 '16 at 07:13

5 Answers5

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.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0
if (reader["Version"] == actualVersion)
{
    while (reader.ReadToFollowing("Input"))
    {
        string value = reader.ReadElementContentAsString();
        // or
        int value = reader.ReadElementContentAsInt();
    }
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
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 );
helpMe
  • 1
  • 1
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);
   }
}
...
huse.ckr
  • 530
  • 12
  • 39
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");
                }
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20