How can I use an Xml attribute value to get an ass

2019-09-06 22:41发布

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();

标签: c# xml linq
3条回答
太酷不给撩
2楼-- · 2019-09-06 23:03

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"

查看更多
Melony?
3楼-- · 2019-09-06 23:06

Try this

            XDocument MyData = XDocument.Load(FILENAME);
            string color = MyData.Descendants("Red").Elements("Shade").Where(y => (int)y.Attribute("id") == 3).FirstOrDefault().Value;​

查看更多
混吃等死
4楼-- · 2019-09-06 23:14

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

查看更多
登录 后发表回答