1

OuterXML of the XMLNode is below

<d:entry id="_6" d:title="آ" xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng"><d:index d:value="آ" d:title="آ" /><h1 xmlns="http://www.w3.org/1999/xhtml">آ</h1><font color="#007000" xmlns="http://www.w3.org/1999/xhtml">interjection</font><br xmlns="http://www.w3.org/1999/xhtml" />
O, oh! (vocative particle)
</d:entry>

I am trying to select like this but it fails. I am able to read attributes and other nodes

var vr_Word_Type = vrNode.SelectSingleNode(".//font", nsmgr);
var vr_Word_Type2 = vrNode.SelectSingleNode(".//font");

Here my full code

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(@"D:\my.xml");

XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmgr.AddNamespace("d", "http://www.apple.com/DTDs/DictionaryService-1.0.rng");

XmlNodeList rssItems = rssDoc.SelectNodes("//d:entry", nsmgr);

foreach (XmlNode vrNode in rssItems)
{
    string srTitle = vrNode.Attributes["d:title"]?.Value;
    var vrSubNodes = vrNode.SelectNodes(".//d:index", nsmgr);
    List<string> lstIndex_Values = new List<string>();
    foreach (XmlNode vrD_Val in vrSubNodes)
    {
        lstIndex_Values.Add(vrD_Val.Attributes["d:value"]?.Value);
    }

    var vr_Word_Type = vrNode.SelectSingleNode(".//font", nsmgr);
    var vr_Word_Type2 = vrNode.SelectSingleNode(".//font");

}
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

2 Answers2

1

You will still need to use the Namespace added in your code above. So something similar to below would be needed.

vrNode.SelectSingleNode("//d:font", nsmgr);

Also, try using /*/, as it can be faster than //.

Mason Mize
  • 211
  • 1
  • 5
1

I couldn't figure out how to use the default namespace, but this way seems to work:

nsmgr.AddNamespace("x", "http://www.w3.org/1999/xhtml");
// ...
var vr_Word_Type = vrNode.SelectSingleNode("x:font", nsmgr);
Slai
  • 22,144
  • 5
  • 45
  • 53