不能超越root,LINQ to(Funds)XML,C#(Cant go deeper than root, LINQ to (Funds)XML, C#)

编程入门 行业动态 更新时间:2024-10-27 02:28:41
不能超越root,LINQ to(Funds)XML,C#(Cant go deeper than root, LINQ to (Funds)XML, C#)

我正在使用特定的FundsXML-Schema尝试获取特定XML文件的所有Assets以进行迭代。

xml文件的简短示例:

<?xml version="1.0" encoding="utf-8"?> <FundsXML xmlns="http://www.fundsxml.org/XMLSchema/3.0.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="3.0.5" xsi:schemaLocation="http://www.fundsxml.org/XMLSchema/3.0.5 FundsXML3.0.5.xsd"> <Date>2015-02-27</Date> ... <AssetMasterData> <Asset> <SecurityCodes> <ISIN>XXXXXXXXXXXX</ISIN> </SecurityCodes> </Asset> ... <Asset> </AssetMasterData> </FundsXML>

我想在那里迭代Asset 。 我试过了:

XDocument xmlTree = XDocument.Load(xmlPath); XElement root = xmlTree.Root; foreach (XElement f in root.Descendants()) { System.Windows.MessageBox.Show(f.Name.ToString() +" ; "+f.Value.ToString()); }

输出:{ http://www.fundsxml.org/XMLSchema/3.0.5 }日期; 2015年2月27日

第二部分是读取每个Asset节点的ISIN。 但我没有时间这样做,因为我在第一部分失败了。

编辑:

解决方案是搜索命名空间+名称:

foreach (XElement f in root.Descendants("{http://www.fundsxml.org/XMLSchema/3.0.5}Asset"))

我认为最好的解决方案:

foreach (XElement f in root.Descendants(xmlTree.Root.GetDefaultNamespace()+"Asset"))

I'm working with a specific FundsXML-Schema trying to get all Assetss of a specific XML-File to iterate through.

Short example of xml-file:

<?xml version="1.0" encoding="utf-8"?> <FundsXML xmlns="http://www.fundsxml.org/XMLSchema/3.0.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="3.0.5" xsi:schemaLocation="http://www.fundsxml.org/XMLSchema/3.0.5 FundsXML3.0.5.xsd"> <Date>2015-02-27</Date> ... <AssetMasterData> <Asset> <SecurityCodes> <ISIN>XXXXXXXXXXXX</ISIN> </SecurityCodes> </Asset> ... <Asset> </AssetMasterData> </FundsXML>

I want to iterate through Assets in there. I tried:

XDocument xmlTree = XDocument.Load(xmlPath); XElement root = xmlTree.Root; foreach (XElement f in root.Descendants()) { System.Windows.MessageBox.Show(f.Name.ToString() +" ; "+f.Value.ToString()); }

Output: {http://www.fundsxml.org/XMLSchema/3.0.5}Date ; 2015-02-27

The second part would be to read ISIN of each Asset node. But I hadn't time to do this, because I'm failing at the first part.

EDIT:

Solution was to search for namespace+name:

foreach (XElement f in root.Descendants("{http://www.fundsxml.org/XMLSchema/3.0.5}Asset"))

Best solution in my opinion:

foreach (XElement f in root.Descendants(xmlTree.Root.GetDefaultNamespace()+"Asset"))

最满意答案

由于XML位于命名空间中,因此需要将命名空间信息添加到Descendants查询中。

你可以在这里看到一个例子

你可以尝试获得

roots.Descendants()

不过滤并检查它返回的节点以确认这一点。

As your XML is in a namespace, you need to add the namespace information to the Descendants query.

You can see an example here

You can try to get the

roots.Descendants()

Without filtering and check the nodes that it returns to confirm this.

更多推荐

本文发布于:2023-07-26 00:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LINQ   root   Funds   deeper   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!