Can't get XPathSelectElements to work with XElement
I am creating an in-memory Xml tree using XElement. Here is a sample of my xml:
<Curve>
<Function>createCurve</Function>
<Parameters>
<Input>
<BaseCurve>
<CurveType Type="String">16fSmoothCurve</CurveType>
<Ccy Type="String">USD</Ccy>
<Tenors>
<Item Type="String">1M</Item>
<Item Type="String">3M</Item>
<Item Type="String">1U</Item>
<Item Type="String">Z1</Item>
</Tenors>
<Rates>
<开发者_JS百科Item Type="String">.02123</Item>
<Item Type="String">.02214</Item>
<Item Type="String">.021234</Item>
<Item Type="String">.02674</Item>
</Rates>
</BaseCurve>
</Input>
</Parameters>
</Curve>
I am creating the xml by chaining together XElements. For example,
var root = new XElement("Curve",
new XElement("Function", "createCurve"),
new XElement("Parameters"), etc);
I would then like to query the XElement via XPath. For example,
var tenors = root.XPathSelectElements("//Tenors/Item");
var rates = root.XPathSelectElements("//Rates/Item");
I can successfully select a single element, for example,
var firstTenor = root.XPathSelectElement("//Tenors/Item");
var firstRate = root.XPathSelectElement("//Rates/Item");
However, trying to select multiple elements give me 0 results.
I've tried creating an XDocument and querying off of that however I get the same results. I've also tried adding an XDeclaration to the beginning of the tree but no luck.
Why can I not query multiple elements from my XElement tree?
Thanks!
Drew
Use XmlNodeList:
XmlNodeList nodesXml = root.SelectNodes("//Tenors/Item");
foreach (XmlNode item in nodList)
{
var tenors = item.InnerText;
}
That what I do, and it works perfect.
精彩评论