0

I have a large Xml file containing my data. I need to programmatically get data from it. Here is a much smaller file but with the exact same structure ...

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<colours xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Red>
    <Shade id="1">
      <colour>crimson</colour>
    </Shade>
    <Shade id="2">
      <colour>raspberry</colour>
    </Shade>
    <Shade id="3">
      <colour>lava</colour>
    </Shade>
    <Shade id="4">
      <colour>scarlet</colour>
    </Shade>
  </Red>
  <Green>
    <Shade id="1">
      <colour>asparagus</colour>
    </Shade>
    <Shade id="2">
      <colour>emerald</colour>
    </Shade>
    <Shade id="3">
      <colour>lime</colour>
    </Shade>
    <Shade id="4">
      <colour>avocado</colour>
    </Shade>
  </Green>
  <Blue>
    <Shade id="1">
      <colour>cyan</colour>
    </Shade>
    <Shade id="2">
      <colour>sapphire</colour>
    </Shade>
    <Shade id="3">
      <colour>powder</colour>
    </Shade>
    <Shade id="4">
      <colour>iris</colour>
    </Shade>
  </Blue>
</colours>

I need to resolve an individual shade, knowing the colour e.g. "Red" and the Shade id e.g."3".

The following code counts the number of shade elements, which is something I need but anything beyond this is still a mystery to me.

    string filepath = "C:/Documents/Data.xml";
    XElement MyData = XElement.Load(filepath);

    int count = MyData.Elements("Red")
                         .Elements("shade")
                         .Count();

    Console.Write(count);
    Console.ReadKey();

3 Answers3

1

If I understand correctly, you'd like to find how many elements have a given Shade id.

You can use XPath queries against Linq to XML's XElement.

Try this:

public void FindShades()
{
    string exePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    string filepath = exePath + @"\XML\Colours.xml";
    XElement MyData = XElement.Load(filepath);
    IEnumerable<XElement> data = MyData.XPathSelectElements("//Shade[@id='3']");
    int count = data.Count();                                 

}

Let me know if this is what you want. If not, please comment and I'll modify my answer.

Cheers

Luc Morin
  • 5,302
  • 20
  • 39
  • Counting the elements is definitely one of my requirements however the key requirement is that if I know the colour (e.g. "Red") and if I know the id (e.g. "3") how can I return the name text, which in this case would be "lava"? – Paul Rathbone Oct 19 '15 at 09:56
  • also at @har07 - can you point me to any useful web resources to learn how to use Linq to Xml and XPath queries? – Paul Rathbone Oct 19 '15 at 09:59
  • the above also @jdweng – Paul Rathbone Oct 19 '15 at 10:00
1

Try this

            XDocument MyData = XDocument.Load(FILENAME);
            string color = MyData.Descendants("Red").Elements("Shade").Where(y => (int)y.Attribute("id") == 3).FirstOrDefault().Value;​
jdweng
  • 33,250
  • 2
  • 15
  • 20
1

Besides using XPath predicate, as demonstrated in the other answer, you can use LINQ Where() method to filter element for certain criteria :

int count = MyData.Elements("Red")
                  .Elements("Shade")
                  .Where(o => (int?)o.Attribute("id") == 3)
                  .Count();

Dotnetfiddle Demo

Notice that XML is case-sensitive, so "Shade" is not equal to "shade"

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thank you for that. The S/shade got lost in transcription. Thank you also for turning me on to dotnetfiddle, my life is now complete. – Paul Rathbone Oct 19 '15 at 09:42