0

Here is the XML

<?xml version="1.0" encoding="UTF-8"?>
<Data_Speed>
<Tech ID = "UMTS">
    <Coverage ID="Dense_Urban">
        <DownLoad_Speed>10</DownLoad_Speed>
        <Upload_Speed>20</Upload_Speed>
    </Coverage>
    <Coverage ID="Urban">
        <DownLoad_Speed>30</DownLoad_Speed>
        <Upload_Speed>40</Upload_Speed>
    </Coverage>
    <Coverage ID="SubUrban">
        <DownLoad_Speed>50</DownLoad_Speed>
        <Upload_Speed>60</Upload_Speed>
    </Coverage>
 </Tech>
<Tech ID = "UMTS900">
    <Coverage ID="Dense_Urban">
        <DownLoad_Speed>11</DownLoad_Speed>
        <Upload_Speed>12</Upload_Speed>
    </Coverage>
    <Coverage ID="Urban">
        <DownLoad_Speed>13</DownLoad_Speed>
        <Upload_Speed>14</Upload_Speed>
    </Coverage>
    <Coverage ID="SubUrban">
        <DownLoad_Speed>15</DownLoad_Speed>
        <Upload_Speed>16</Upload_Speed>
    </Coverage>
</Tech>
<Tech ID = "4G800">
    <Coverage ID="Dense_Urban">
        <DownLoad_Speed>30</DownLoad_Speed>
        <Upload_Speed>42</Upload_Speed>
    </Coverage>
    <Coverage ID="Urban">
        <DownLoad_Speed>50</DownLoad_Speed>
        <Upload_Speed>34</Upload_Speed>
    </Coverage>
    <Coverage ID="SubUrban">
        <DownLoad_Speed>45</DownLoad_Speed>
        <Upload_Speed>46</Upload_Speed>
    </Coverage>
    <Coverage ID="Rural">
        <DownLoad_Speed>47</DownLoad_Speed>
        <Upload_Speed>48</Upload_Speed>
    </Coverage>
    <Coverage ID="Variable">
        <DownLoad_Speed>15</DownLoad_Speed>
        <Upload_Speed>52</Upload_Speed>     
    </Coverage>
    <Coverage ID="Outdoor">
        <DownLoad_Speed>25</DownLoad_Speed>
        <Upload_Speed>22</Upload_Speed>
    </Coverage>
</Tech>
</Data_Speed>

So how could I get value of DownLoad_Speed> & UpLoad_Speed> element by given Coverage ID Urban> and Tech ID UMTS900>? Say, if I give tech id value = "UMTS900" and coverage id value "Urban", I'd like to have string value dwnload_speed = 13 and Upload_Speed = 14 as result.

1

4 Answers 4

2

Using Linq to Xml

    XDocument doc = XDocument.Load(filepath);

    //ex...
    string technology = "UMTS900"; 
    string coverage = "Dense_Urban";

    var result = doc.Descendants("Tech")
        .Where(x=> (string)x.Attribute("ID") == technology)
        .Elements("Coverage")
        .Where(x=>(string)x.Attribute("ID")== coverage)
        .Select(x=> new 
                    {
                        Dowload_Speed = (string)x.Element("DownLoad_Speed"),
                        Upload_Speed = (string)x.Element("Upload_Speed")
                    });

Check this Demo

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

4 Comments

Tip: you can make use of the explicit conversions to avoid the null check, e.g. x => (string)x.Attribute("ID") == technology
Thanks Hari Prasad for quick response :)
Can we achieve same thing using Javascript @Hari Prasad?
That is an extended question, I would suggest post new question. It may confuse people reaching this post with similar problem if we include additional implementation here. Hope you understand.
0

You need to open XML document

XmlDocument _document = new XmlDocument();
byte[] bytes = File.ReadAllBytes(filePath);
string xml = Encoding.UTF8.GetString(bytes);
try
{
_document.LoadXml(xml);
}
catch (XmlException e)
{
//exception handling
}                  

var doc = (XmlDocument)_document.CloneNode(true);

XmlNode node = doc.GetElementsByTagName("your child node name");

Once you get your node then you can do necessary thing with it

Comments

0
string techId = "UMTS900";
string coverageId = "SUBURBAN";
int downloadSpeed = 0;
int uploadSpeed = 0;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\....\DataSpeed.xml");
XmlNodeList techTags = xmlDoc.GetElementsByTagName("Tech");

foreach (XmlNode techTag in techTags)
{
   if (techTag.Attributes["ID"].Value.Equals(techId,StringComparison.OrdinalIgnoreCase))
   {
      XmlNodeList coverageTags = techTag.ChildNodes;
      foreach (XmlNode coverageTag in coverageTags)
      {
         if (coverageTag.Attributes["ID"].Value.Equals(coverageId, StringComparison.OrdinalIgnoreCase))
         {
            downloadSpeed =Convert.ToInt16(coverageTag.ChildNodes[0].InnerText);
            uploadSpeed = Convert.ToInt16(coverageTag.ChildNodes[1].InnerText);
            break;
          }
       }
       break;
    }
 }

    if (downloadSpeed == 0 && uploadSpeed == 0)
    {
      Console.WriteLine("Specified Tech Id and Coverage Id not found");
    }
    else
    {
      Console.WriteLine("Download Speed is {0}. Upload Speed is {1}.",downloadSpeed,uploadSpeed);
   }

Comments

0

Here is solution using XmlReader with xml linq

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


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

            XmlReader reader = XmlReader.Create(FILENAME);
            string id = "UMTS900";
            object coverages = null;
            while (!reader.EOF)
            {
                if (reader.Name != "Tech")
                {
                    reader.ReadToFollowing("Tech");
                }
                if (!reader.EOF)
                {
                    XElement tech = (XElement)XElement.ReadFrom(reader);
                    if((string)tech.Attribute("ID") == id)
                    {
                        coverages = tech.Descendants("Coverage").Select(x => new
                        {
                            id = (string)x.Attribute("ID"),
                            downLoad_Speed = (int)x.Element("DownLoad_Speed"),
                            upLoad_Speed = (int)x.Element("Upload_Speed"),
                        }).ToList();
                        break;
                    }
                }
            }



        }
    }

}

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.