无法找到具有后代关键词的元素:XML-> LINQ-C# [英] not able to find elements with Descendants key words: XML->LINQ-C#
问题描述
Hi Firends,
我的要求是:我需要遍历我的xml并应用一些搜索条件。并将结果保存在列表中。
我的问题是:我无法通过 Descendants
选择所有位置节点关键字。
以下是我要求的路径
Envelope-> Body-> GetRequestedDataResponse-> GetRequestedDataResult-> Message-> EBCMessageType-> Message-> ; NewDataSet->职位
我被困在这里 var ac = doc.Root.Descendants(Positions)
请帮忙。
我的XML
< s:信封 xmlns:s = http://schemas.xmlsoap.org/soap/envelope / >
< s:正文 xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xmlns:xsd = http://www.w3.org/2001/XMLSchema >
< GetRequestedDataResponse xmlns = http://tempuri.org/ >
< GetRequestedDataResult >
< ReferenceID xmlns = http://ebc.mssb.com > M99TzPSsNsKxIxfUfhG < / ReferenceID >
< ReceivedTime xmlns = http://ebc.mssb。 com > 2015-07-17T10:18:40.6321762-04:00 < / ReceivedTime >
< CompletedTime xmlns = http://ebc.mssb.co m > 2015-07-17T10:18:41.4491762-04:00 < / CompletedTime >
< StatusCode xmlns = http://ebc.mssb.com > 成功< / StatusCode >
< 消息 xmlns = http://ebc.mssb。 com >
< EBCMessageType >
< 消息 > {CALL GETMM_ACCT_TURBPOS(RSGETMM_ACCT_TURBPOSFAWorkstation} < /消息 >
< MessageType > COMMAND_STRING < / MessageType >
< / EBCMessageType >
< EBCMessageType &g t;
< 消息 >
< NewDataSet >
< 职位 >
< Office > 101 < / Office >
< 帐户 > 10239 < / Account >
< AccruedInterest > 25.32 < / AccruedInterest >
< Cusip > asd < / Cusip >
< PriceFactor > 1.0 < / PriceFactor >
< 数量 > 5 < 跨度> / Quantity >
< /职位 >
< 职位 >
< Office > 101 < / Office >
< 帐户 > 10269 < / account >
< AccruedInterest > 45.34 < / AccruedInterest >
< Cusip > pqr < / Cusip >
< PriceFactor > 1.0 < / PriceFactor >
< 数量 > 45 < /数量 >
< /职位 >
< / NewDataSet >
< /消息 >
< MessageType > RAW_DATA < / MessageType >
< / EBCMessageType >
< /消息 >
< < span class =code-leadattribute> / GetRequestedDataResult >
< / GetRequestedDataResponse >
< / s:正文 >
< / s:信封 >
c#code
public static 列表< AccruedClass> validaccrued()
{
XDocument doc = XDocument.Load( SampleXML.xml跨度>);
var ac = doc.Root.Descendants( 职位)
。其中(item = > ( float ?)item.Element( AccruedInterest)!= null &&( float ?)item.Element( AccruedInterest)> 0 。 0 )
。选择(item = > new AccruedClass
{
Cusip = item.Element( Cusip)。值!= null ? item.Element( Cusip)。值: ,
// Symbol = item.Element(symbol)。Value,
AccruedInterest = item.Element( AccruedInterest)。值!= null ? Convert.ToDouble(item.Element( AccruedInterest)。Value): 0 ,
PriceFactor = item.Element( PriceFactor)。值!= null ? Convert.ToDouble(item.Element( PriceFactor)。Value): 0 ,
数量= item.Element( 数量)。值!= null ? Convert.ToDouble(item.Element( Quantity)。Value): 0 ,
})
.ToList();
List< AccruedClass> list_accr = ac.ToList< AccruedClass>();
return list_accr;
}
static void Main( string [] args)
{
列表与LT; AccruedClass> ac = new 列表< AccruedClass>();
ac = validaccrued();
}
公开 类 AccruedClass
{
public string Cusip { get ; set ; }
// public string Symbol {get;组; }
public double AccruedInterest {获得跨度>; set ; }
public double PriceFactor { get 跨度>; set ; }
public double 数量{ get 跨度>; set ; }
public double CalcAcc { get 跨度>; set ; }
}
问题是位置元素是在名称空间中,继承自封闭的< Message xmlns =http://ebc.mssb.com>所以更改为包含命名空间信息:
public static 列表< AccruedClass> validaccrued()
{
XDocument doc = XDocument.Load( SampleXML.xml跨度>);
// 下一个字符串表达式应该是单个字符串
// 但CodeProject过于聪明,认为它应该变成活动链接!
XNamespace ns = http + < span class =code-string>://ebc.mssb.com; // 假设它不会因文件而异...
var ac = doc.Root.Descendants(ns + Positions)
.Where(item = > {
float ? accInt =( float ?)item.Element( AccruedInterest );
return accInt!= null &&& accInt。值> 0 .0f;
})
// 等,与之前相同......
Hi Firends,
My requirement is: I need to traverse through my xml and apply some search criteria. and save the result in a List.
My problem is : I not able to select all positions node byDescendants
keyword.
Below is my required path
Envelope->Body->GetRequestedDataResponse->GetRequestedDataResult->Message->EBCMessageType->Message->NewDataSet->Positions
I am stuck with herevar ac = doc.Root.Descendants("Positions")
please help.
My XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <GetRequestedDataResponse xmlns="http://tempuri.org/"> <GetRequestedDataResult> <ReferenceID xmlns="http://ebc.mssb.com">M99TzPSsNsKxIxfUfhG</ReferenceID> <ReceivedTime xmlns="http://ebc.mssb.com">2015-07-17T10:18:40.6321762-04:00</ReceivedTime> <CompletedTime xmlns="http://ebc.mssb.com">2015-07-17T10:18:41.4491762-04:00</CompletedTime> <StatusCode xmlns="http://ebc.mssb.com">Success</StatusCode> <Message xmlns="http://ebc.mssb.com"> <EBCMessageType> <Message>{ CALL GETMM_ACCT_TURBPOS (RSGETMM_ACCT_TURBPOSFAWorkstation}</Message> <MessageType>COMMAND_STRING</MessageType> </EBCMessageType> <EBCMessageType> <Message> <NewDataSet> <Positions> <Office>101</Office> <Account>10239</Account> <AccruedInterest>25.32</AccruedInterest> <Cusip>asd</Cusip> <PriceFactor>1.0</PriceFactor> <Quantity>5</Quantity> </Positions> <Positions> <Office>101</Office> <Account>10269</Account> <AccruedInterest>45.34</AccruedInterest> <Cusip>pqr</Cusip> <PriceFactor>1.0</PriceFactor> <Quantity>45</Quantity> </Positions> </NewDataSet> </Message> <MessageType>RAW_DATA</MessageType> </EBCMessageType> </Message> </GetRequestedDataResult> </GetRequestedDataResponse> </s:Body> </s:Envelope>
c# code
public static List<AccruedClass> validaccrued() { XDocument doc = XDocument.Load("SampleXML.xml"); var ac = doc.Root.Descendants("Positions") .Where(item => (float?)item.Element("AccruedInterest") != null && (float?)item.Element("AccruedInterest") > 0.0) .Select(item => new AccruedClass { Cusip = item.Element("Cusip").Value != null ? item.Element("Cusip").Value : "", // Symbol = item.Element("symbol").Value, AccruedInterest = item.Element("AccruedInterest").Value != null ? Convert.ToDouble(item.Element("AccruedInterest").Value) : 0, PriceFactor = item.Element("PriceFactor").Value != null ? Convert.ToDouble(item.Element("PriceFactor").Value) : 0, Quantity = item.Element("Quantity").Value != null ? Convert.ToDouble(item.Element("Quantity").Value) : 0, }) .ToList(); List<AccruedClass> list_accr = ac.ToList<AccruedClass>(); return list_accr; }
static void Main(string[] args) { List<AccruedClass> ac = new List<AccruedClass>(); ac = validaccrued(); }
public class AccruedClass { public string Cusip { get; set; } // public string Symbol { get; set; } public double AccruedInterest { get; set; } public double PriceFactor { get; set; } public double Quantity { get; set; } public double CalcAcc { get; set; } }解决方案The problem is that the "Positions" elements are in a namespace, inherited from the enclosing <Message xmlns="http://ebc.mssb.com"> so change to include the namespace info:
public static List<AccruedClass> validaccrued() { XDocument doc = XDocument.Load("SampleXML.xml"); // This next string expression SHOULD be a single string // but CodeProject tries to be too smart and thinks it should be turned into an active link! XNamespace ns = "http"+"://ebc.mssb.com"; // assuming it doesn't vary from file to file... var ac = doc.Root.Descendants(ns + "Positions") .Where(item => { float? accInt = (float?)item.Element("AccruedInterest"); return accInt != null && accInt.Value > 0.0f; }) // etc, same as before...
这篇关于无法找到具有后代关键词的元素:XML-> LINQ-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!