我正在使用特定的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.
更多推荐
发布评论