解析后无法从XML访问所有子元素 [英] Unable to access all child elements from XML after parsing

查看:63
本文介绍了解析后无法从XML访问所有子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我能够使用xDocument解析xml,但它只返回多个子元素的第一个元素。

我的xml如下所示。所以我只获得特定策略的第一个物理和曝光值,即使它有五个值。

 <   stratergies  >  
< ; strtergy >
< 名称 > ... < / name >
< physicals > ..
< physical > ... < / physical >
< 曝光 > ... < / exposure >
< physical > ... < / physical >
< < span class =code-leadattribute>曝光 > ... < < span class =code-leadattribute> / exposure >
< physical > ... < / physical >
< 曝光 > .. 。< / exposure >
< / physicals >
< / strtergy >
< strtergy >
< 名称 > ; ... < / name >
< physicals > ..
< physical > ... < / physical >
< < span class =code-leadattribute>曝光 > ... < < span class =code-leadattribute> / exposure >
< physical > ... < / physical >
< 曝光 > ... < / exposure >
< / physicals >
< / strtergy >
< strtergy >
< 名称 > ... < / name > ;
< 物理 > ..
< physical > ... < / physical >
< 曝光 > ... < / exposure >
< < span class =code-leadattribute> / physicals >
< / strtergy >
< / stratergies >





我的尝试:



解决方案1>

XDocument xDoc = XDocument.Parse(strxml);

var strategylist =(来自i的xDoc.Root.Elements(sch +strategy)

让r = i.Element(sch +physicals)

选择新的

{

StrategyName = (字符串)i.Element(sch +name),

Physical = r.Descendants(sch +physical),

Exposure = r.Descendants(sch +曝光)

})。ToList();





2>



var strategylist =来自i的xDoc.Descendants(sch +策略)

选择新的

{

StrategyName = i.Element(sch +name)!= null? i.Element(sch +name)。值:,

物理= i.Element(sch +physicals)!= null? (i.Element(sch +physicals)。元素(sch +physical)!= null?i.Element(sch +physicals)。元素(sch +physical)。值:): ,

曝光= i.Element(sch +physicals)!= null? (i.Element(sch +physicals)。元素(sch +曝光)!= null?Double.Parse(i.Element(sch +physicals)。元素(sch +曝光)。值) :0):0,

};

解决方案

var strategylist =(from x in xDoc.Root.Elements(strategy))

让r = i.Elements(物理)

选择新的

{

StrategyName = i。元素(名称)!= null?i.Element(sch +name)。值:,

物理= r.Elements(物理)。元素(物理) ).Elements(physical)。ToList(),

Exposure = r.Elements(physicals)。Elements(exposure)。ToList()

}。ToList();



dgStrategy.DataSource = strategylist;

dgStrategy.DataBind();

proxy.Close();


试试这段代码。这是对您发布的代码的略微修改。

更正:

1.您没有在查询中使用拼写错误的元素。 策略应该是 strtergy

(这就是为什么让错误拼写的变量和名称漏掉的原因)

2。代码

 r.Descendants(sch +  曝光



将为您提供整个节点,而不是节点的内容。

使用

 r.Element(  physical)。值

代替。

3.我不知道 sch 应该是什么意思,所以我删除了它。工作得更好。



[更新]改为使用let r = i。元素(物理)而不是元素。 />

 XDocument xDoc = XDocument.Parse(strxml); 
var strategylist =( from i in xDoc.Root.Elements( strtergy
r = i。元素 physicals
选择 new
{
StrategyName = i.Element( name)。值,
物理= r.Elements(< span class =code-string> physical)。ToList(),
Exposure = r.Elements( 曝光)。ToList()
})。ToList();





[UPDATE2]以逗号分隔的列表获取多个结果

 XDocument xDoc = XDocument.Parse(strxml); 
var strategylist =
来自 i in xDoc.Root.Elements( strtergy
< span class =code-sdkkeyword> let r = i.Elements( physicals
选择 new
{
StrategyName = i.Element ( name)。值,
物理= string .Join( ,r。元素( physical)。选择(x = > x .Value)),
Exposure = string .Join( ,r。元素( 曝光)。选择(x = > x.Value))
})。ToList();

var physical1 = strategylist [ 0 ]。物理;
将包含: ...,...,...


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.

<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,
};

解决方案

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();


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

r.Descendants(sch + "exposure")


will give you the whole node, not the content of the node.
Use

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.

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

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: "...,...,..."


这篇关于解析后无法从XML访问所有子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆