-2
<?xml version="1.0" encoding="UTF-8"?>
<meta>
    <field type="xs-string" name="AssetId">TF00000002</field>
    <field type="xs-string" name="Title">TitleOfAsset</field>
</meta>

I have this XML loaded in to a XDocument using the function

XDocument doc = XDocument.Parse(xmlData)

However, I want to be able to retrieve the text fields "TF00000002" and "TitleOfAsset" ... How do I go about doing this?

templateMetaData.assetID = doc
    .Descendants()
    .Where(p => p.Name.LocalName == "AssetId")
    .ToString();

returns:

 System.Linq.Enumerable+WhereEnumerableIterator`1[System.Xml.Linq.XElement] 

Can anyone shine a light on this?

James Brankin
  • 525
  • 4
  • 7
  • `Doesn't work.` is not very precise... can you describe what exactly happens? – Steve B Jun 27 '14 at 13:39
  • 1
    "Doesn't work" is far too vague a description. What happened? (Hint: `Where` returns a sequence, not a single value, and you're currently looking at the element name, not the `name` attribute). – Jon Skeet Jun 27 '14 at 13:39
  • Sorry, yes. It returns "System.Linq.Enumerable+WhereEnumerableIterator`1[System.Xml.Linq.XElement]" instead of the element. – James Brankin Jun 27 '14 at 13:40
  • possible duplicate of [Parsing XML using XDocument](http://stackoverflow.com/questions/7798852/parsing-xml-using-xdocument) – PiotrWolkowski Jun 27 '14 at 14:00

1 Answers1

0

In your query, you are calling ToString on an IEnumerable<XElement> which will never give you the expected result, instead look for field elements under your Root and get their value:

var values = doc.Root
           .Elements("field")
           .Select(element => (string)element);

If you want to access your values using the name attribute you can use Dictionary:

var values = doc.Root
           .Elements("field")
           .ToDictionary(x => (string)x.Attribute("name"), x => (string)x);

Then you can access the value of AssetId:

var id = values["AssetId"];
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • I think `element => element.Value` would make more sense. Just a suggestion. – gunr2171 Jun 27 '14 at 13:41
  • This code just tells me that the sequence contains no elements whenever I change "field" to "AssetId" edit: just saw your edit... trying that now – James Brankin Jun 27 '14 at 13:50
  • @user3780459 because AssetId is the value of your name attribute. it is **not** an XElement – Selman Genç Jun 27 '14 at 13:52
  • OK so now I have this code instead var assetID = doc.Root.Elements("xs-string").ToDictionary(x => (string)x.Attribute("AssetId"), x => (string)x); and it also doesn't return the value of the AssetId. Am I doing something wrong still? – James Brankin Jun 27 '14 at 14:02
  • are you kidding with me ? why did you write .Elements("xs-string") ? do you see anything like that in my answer ? – Selman Genç Jun 27 '14 at 14:03
  • sorry... I didn't read you correctly. Thank you for your answer. – James Brankin Jun 27 '14 at 14:25
  • Your first example will also select the TEXT() of any children elements. The OP's sample doesn't have any, but other's might. – Developer Webs Sep 09 '21 at 15:21