Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm able to parse xml using xDocument but it returns only the first element of multiple child elements.
My xml looks like below. so I'm getting only first physical and exposure value for a particular strategy even though it has five values.
XML
<stratergies>
    <strtergy>
        <name>...</name>
        <physicals>..
        	<physical>...</physical>
        	<exposure>...</exposure>
		<physical>...</physical>
        	<exposure>...</exposure>
		<physical>...</physical>
        	<exposure>...</exposure>
        </physicals>
    </strtergy>
<strtergy>
        <name>...</name>
        <physicals>..
        	<physical>...</physical>
        	<exposure>...</exposure>
		<physical>...</physical>
        	<exposure>...</exposure>
        </physicals>
    </strtergy>
<strtergy>
        <name>...</name>
        <physicals>..
        	<physical>...</physical>
        	<exposure>...</exposure>
        </physicals>
    </strtergy>
</stratergies>


What I have tried:

solution 1>
XDocument xDoc = XDocument.Parse(strxml);
var strategylist = (from i in xDoc.Root.Elements(sch + "strategy")
let r = i.Element(sch + "physicals")
select new
{
StrategyName = (string)i.Element(sch + "name"),
Physical = r.Descendants(sch + "physical"),
Exposure = r.Descendants(sch + "exposure")
}).ToList();


2>

var strategylist = from i in xDoc.Descendants(sch + "strategy")
select new
{
StrategyName = i.Element(sch + "name") != null ? i.Element(sch + "name").Value : "",
Physical = i.Element(sch + "physicals") != null ? (i.Element(sch + "physicals").Element(sch + "physical") != null ? i.Element(sch + "physicals").Element(sch + "physical").Value : "") : "",
Exposure = i.Element(sch + "physicals") != null ? (i.Element(sch + "physicals").Element(sch + "exposure") != null ? Double.Parse(i.Element(sch + "physicals").Element(sch + "exposure").Value) : 0) : 0,
};
Posted
Updated 3-Jun-16 17:17pm
v2

var strategylist = (from i in xDoc.Root.Elements("strategy")
let r = i.Elements("physicals")
select new
{
StrategyName = i.Element("name") != null ? i.Element(sch + "name").Value:"",
Physical = r.Elements("physicals").Elements("physical").Elements("physical").ToList(),
Exposure = r.Elements("physicals").Elements( "exposure").ToList()
}).ToList();

dgStrategy.DataSource = strategylist;
dgStrategy.DataBind();
proxy.Close();
 
Share this answer
 
Try this code. It is a slight modification of your posted code.
Corrections:
1. You didn't use the misspelled element in your query. strategy should be strtergy
(This is why it is a bad idea to let misspelled variables and names slip through)
2. The code
C#
r.Descendants(sch + "exposure")

will give you the whole node, not the content of the node.
Use
C#
r.Element("physical").Value
instead.

3. I have no idea what sch is supposed to mean, so I removed it. Works much better.

[UPDATE] Changed to use let r = i.Elements("physicals") instead of Element.
C#
XDocument xDoc = XDocument.Parse(strxml);
var strategylist = (from i in xDoc.Root.Elements("strtergy")
                    let r = i.Elements("physicals")
                    select new
                    {
                        StrategyName = i.Element("name").Value,
                        Physical = r.Elements("physical").ToList(),
                        Exposure = r.Elements("exposure").ToList()
                    }).ToList();


[UPDATE2] Get multiple results as a comma separated list
C#
XDocument xDoc = XDocument.Parse(strxml);
var strategylist = 
    (from i in xDoc.Root.Elements("strtergy")
    let r = i.Elements("physicals")
    select new
    {
        StrategyName = i.Element("name").Value,
        Physical = string.Join(",", r.Elements("physical").Select(x => x.Value)),
        Exposure = string.Join(",", r.Elements("exposure").Select(x => x.Value))
    }).ToList();

var physical1 = strategylist[0].Physical;
Will contain: "...,...,..."
 
Share this answer
 
v3
Comments
shwetakeshri 3-Jun-16 8:42am    
Hi George,
Sorry my bad spelling was not an issue ..that was copy paste error while posting question.
"sch" is namespace....I'm able to access strategy, physical and exposure using Element . My issue is that only one element for each strategy is accessible. In case I have multiple physical and exposure for same strategy it returns only first physical and exposure ..since we are using "Element"...which returns only first element from collection. Tried with "Elements" as well but no luck..
George Jonsson 3-Jun-16 8:56am    
I think I missed to read that part of the question.
I will have another look.
George Jonsson 3-Jun-16 9:09am    
Not sure exactly what output you want, but I have changed the code so it gives you all sub-elements.
shwetakeshri 3-Jun-16 23:15pm    
Hi George,
Tried with updated code with Elements but it returns only strategy value. I want to display Strategy, Physical and Exposure in a Grid. But with above code it only displays Strategy..moreover how do i check null for physical and exposure as i have done for strategy in below solution
Can you please help me.
George Jonsson 4-Jun-16 0:15am    
So how do you want the values for Physical and Exposure to be presented? As comma separated lists?
You have one Strategy element and many Physical and Exposure elements, so you cannot get them as one value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900