473,394 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

linq question

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.
Jun 27 '08 #1
1 1610
CSharper <cs******@gmx.comwrote:
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);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Johnson | last post by:
Greetings, i want to be able to use linq new technology with sql server. the senario i am not able to do so far is as follow: 1- i program with linq 2- be able to generate and manipulate...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
1
by: shapper | last post by:
Hi, I wonder, is there some tool that transforms SQL procedures to LINQ? :-) I want to use LINQ but I have so much work done in SQL that would be great to transform my SQL code to LINQ. ...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
1
by: hrishy | last post by:
Hi Terry Oops i never realised the mistake i have commited I apologise (i thought changing the subject line would make a new thread) I apologise (I thought Python programmers were smart and...
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
4
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.