linq question

C

CSharper

I have an xml file the following

<?xml version="1.0" encoding="utf-8" ?>
<Source Met="Source">
<Name SourceName="0001">
<Location>Texas</Location>
<Values>
<Value id="1" Amount="1.0" />
<Value id="2" Amount="2.0" />
</Values>
</Name>
<Name SourceName="0002">
<Location>Texas</Location>
<Values>
<Value id="10" Amount="1.0" />
<Value id="20" Amount="2.0" />
</Values>
</Name>
</Source>

I want to convert this XML into a normalised string and I would like
the string result to be
"Source", "0001", "Texas", "1", "1.0"
"Source", "0001", "Texas", "1", "2.0"
"Source", "0002", "Texas", "10", "1.0"
"Source", "0002", "Texas", "20", "2.0"

What is the best way to apprach this problem? I did the following
var list = from c in doc.decentents()
where !c.HasElements()
select c;

with this I can get everything except location properly, I am sure I
am missing something simple here.
Thanks.
 
J

Jon Skeet [C# MVP]

CSharper said:
I have an xml file the following

<?xml version="1.0" encoding="utf-8" ?>
<Source Met="Source">
<Name SourceName="0001">
<Location>Texas</Location>
<Values>
<Value id="1" Amount="1.0" />
<Value id="2" Amount="2.0" />
</Values>
</Name>
<Name SourceName="0002">
<Location>Texas</Location>
<Values>
<Value id="10" Amount="1.0" />
<Value id="20" Amount="2.0" />
</Values>
</Name>
</Source>

I want to convert this XML into a normalised string and I would like
the string result to be
"Source", "0001", "Texas", "1", "1.0"
"Source", "0001", "Texas", "1", "2.0"
"Source", "0002", "Texas", "10", "1.0"
"Source", "0002", "Texas", "20", "2.0"

What is the best way to apprach this problem? I did the following
var list = from c in doc.decentents()
where !c.HasElements()
select c;

with this I can get everything except location properly, I am sure I
am missing something simple here.

You need to go "up" from Value to Values, then "up" from Values to
Name, then *down* again to find the Location.

Here's a short but complete program which does it all:

using System;
using System.Linq;
using System.Xml.Linq;

class Test
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("test.xml");

var entries =
from valueElt in doc.Descendants("Value")
let nameElt = valueElt.Parent.Parent
select new { Source=(string) nameElt.Parent.Attribute("Met"),
Name=(string)nameElt.Attribute("SourceName"),
Location=(string)nameElt.Element("Location"),
Id=(int) valueElt.Attribute("id"),
Amount=(decimal) valueElt.Attribute("Amount") };

foreach (var entry in entries)
{
Console.WriteLine(entry);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Create a complex type from Linq-to-XML 2
XSD question 10
Sub select in LINQ 1
XML to Dictionary using LINQ 2
Object store/Database and LINQ 2
Drop down-list translation 2
linq question 4
Linq. Where 1

Top