2

Good Day,

I have been playing with the ToDicationary() extension method

var document = XDocument.Load(@"..\..\Info.xml");
XNamespace ns = "http://www.someurl.org/schemas";

var myData = document.Descendants(ns + "AlbumDetails").ToDictionary
    (
        e => e.Name.LocalName.ToString(),
        e => e.Value
    );

Console.WriteLine("Writing music...");
foreach (KeyValuePair<string, string> kvp in myData)
{
    Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value);
}

with the following XML data:

<?xml version="1.0" encoding="UTF-8"?>
<Database xmlns="http://www.someurl.org/schemas">
    <Info>
        <AlbumDetails>
            <Artist>Ottmar Liebert</Artist>
            <Song>Barcelona Nights</Song>
            <Origin>Spain</Origin>
        </AlbumDetails>
    </Info>
</Database>

and I'm not getting the output I want. Instead I'm getting this:

Writing music...
AlbumDetails = Ottmar LiebertBarcelona NightsSpain

Instead, I want myData("Artist") = "Ottmar Liebert", etc...

Is it possible to do with Descendants?

TIA,

coson

1
  • I don't think you can create a dictionary from AlbumDetails's descendants. Artist,Song,Origin might happen not to be unique. Commented Aug 16, 2012 at 19:56

2 Answers 2

2

The following will simply get the AlbumDetails node:

document.Descendants(ns + "AlbumDetails")

You want its direct descendants (child nodes) - since these are also elements:

document.Descendants(ns + "AlbumDetails").Elements()

The full line would be:

var myData = document.Descendants(ns + "AlbumDetails")
             .Elements().ToDictionary(
                                      e => e.Name.LocalName.ToString(),
                                      e => e.Value
                                     );
Sign up to request clarification or add additional context in comments.

5 Comments

Maybe not descendants but rather children (elements).
@JeffMercado - I don't believe Children is an extension method in LINQ to XML.
What I meant was that he probably wants to grab only the child elements of the AlbumDetails nodes, so he'd need the Elements() method, not DescendantNodes().
@JeffMercado - Thanks for that, just as I found the documentation myself. Have not played with LINQ 2 XML much :)
Descendants will bring all childs (including sub childs) while Elements brings only immediate child. so it is safe to use Elements most cases. That said it depends on how the OP wants to take his data. But worth to mention
1

try this.

string s = "<data><resource key=\"123\">foo</resource><resource key=\"456\">bar</resource><resource key=\"789\">bar</resource></data>"; 
XmlDocument xml = new XmlDocument(); 
xml.LoadXml(s); 
XmlNodeList resources = xml.SelectNodes("data/resource"); 
SortedDictionary<string,string> dictionary = new SortedDictionary<string,string>(); 
foreach (XmlNode node in resources){ 
    dictionary.Add(node.Attributes["key"].Value, node.InnerText); 
} 

2 Comments

Seems like you answer your own question instead of OP's one.
@L.B - Looks like answering the general question, using their own XML instead of that of the OP... And using XmlDocument instead of XDocuement, as the OP has.

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.