1

I have an xml document in the following format.

<?xml version="1.0" encoding="UTF-8" ?>

<Rows>
<Row>    
    <Field Name='PhysicalLocation'>11;#West</Field>
    <Field Name='ID'>3327</Field>
</Row>
</Rows>

And am attempting to do a linq selection with it.

I have tried the following.

XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");

var query = from item in xmlDoc.Descendants("Rows").Elements()
                    select new { ID = item.Attribute("ID").Value, Value = item.Attribute("PhysicalLocation").Value };

And also

XDocument xmlDoc = XDocument.Load("C:\\manifest.xml");

var query = from item in xmlDoc.Descendants("Rows").Elements()
                    select new { ID = item.Element("ID"), Value = item.Element("PhysicalLocation") };

And in both cases I seem to be coming up short. It is generating the expected amount of rows but the values are not being filled.

Could anyone point me in the right direction? What am I missing?

4 Answers 4

1

How about trying a query like this:

var query =
    from item in xmlDoc.Descendants("Rows").Elements()
    let values = item.Elements("Field")
        .ToDictionary(x => x.Attribute("Name").Value, x => x.Value)
    select new
    {
        ID = values["ID"],
        Value = values["PhysicalLocation"],
    };
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your input but unfortunately with the above code I'm receiving the following error: {"The given key was not present in the dictionary."}
@MaximGershkovich - I used your example XML provided in your question and it worked fine for me. Let me know if you have bad XML or if you have slightly more complicated requirements.
My bad, I had some bad data (I had a 40mb XML dataset). You're correct, your code works great. Thank you...
1

you don't have an attribute called 'PhysicalLocation' or 'ID'. you only have attributes called 'Name'.

You need to add a where clause on the value of the 'name' attribute to find your ID and PhysicalLocation

Comments

0

I think this should do the trick:

var xDoc = XDocument.Parse(
@"<?xml version='1.0' encoding='UTF-8' ?> 

<Rows> 
    <Row>     
        <Field Name='PhysicalLocation'>11;#West</Field> 
        <Field Name='ID'>3327</Field> 
    </Row> 
</Rows>");

var res = from row in xDoc.Root.Elements("Row")
          select new
          {
              ID = (int)row.Elements("Field").Single(e => (string)e.Attribute("Name") == "ID"),
              PhysicalLocaltion = (string)row.Elements("Field").Single(e => (string)e.Attribute("Name") == "PhysicalLocation"),
          };

foreach (var r in res)
{
    Console.WriteLine(r);
}

Here is the result printed by the loop:

{ ID = 3327, PhysicalLocaltion = 11;#West }

Comments

0

Try this:

        var xd = XDocument.Load("C:\\manifest.xml");
        var query = xd.Root.Descendants("Row").Elements("Field")
            .Select(s => new
            {
                Name = (string)s.Attribute("Name"),
                Value = s.Value
            });

The above code, loops through each "Row" element, then reads the "Field" element data. It will return the following anonymous list:

Name = PhysicalLocation
Value = 11;#West
Name = ID
Value = 3327 

To loop through the query, you can use the following code:

        var sb = new StringBuilder();
        foreach (var i in query)
        {
            sb.Append("\n Name = ").Append(i.Name).Append("\n Value = ").Append(i.Value);
        }

Finally, to find the Field element value by Name, you can use the following query:

var query2 = query.Where(w => w.Name == "ID").Single().Value;

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.