0

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"))
ZonRian
  • 3
  • 2

2 Answers2

0

Based on the sample data you've provided

<Asset></Asset>

doesn't appear to have any data in it. You would need to get

foreach (XElement f in root.Descendants("ISIN"))

I think anyway. If there's no actual text then you will get a blank or empty value?? So it sounds like it's returning what you're asking for??

Frank Pytel
  • 127
  • 16
  • Yeah the sample is very short, because the original File has over 2000 rows. I just wanted to show the structure of the XML. root.Descendants("ISIN") is null (like all other tries of mine) and not an empty string or something like that – ZonRian Mar 12 '15 at 11:11
  • You said it's returning empty, not null. Have you tried going to ISIN? – Frank Pytel Mar 12 '15 at 11:12
0

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.

Juan
  • 3,675
  • 20
  • 34
  • But isn't the user looking for the element value, not the element name?? EDIT: so if you look for descendants() without specifying the element name it will dig down to the first element that has a value?? – Frank Pytel Mar 12 '15 at 11:10
  • 1
    Yeah, but to get the value he must first address the elements by their right name. He is not finding any "Asset" to iterate over because, I think, he is not looking them by their complete namespace+element name. EDIT: Descendants() will return ALL elements under root. – Juan Mar 12 '15 at 11:12