0

I need some help to check if a node exist.

I can select the node like this

node["sa:name1"]["sa:name2"]["sa:name3"]

And this works fine but if the node doesn't exsist I get an error I have tried this

if(node.SelectSingleNode("/sa:name1/sa:name2/sa:name3") != null)

but this dident help this just makes a new error

An exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll but was not handled in user code

Additional information: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

3 Answers3

1

Use http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.addnamespace.aspx

XmlNamespaceManager nsMgr = new XmlNamespaceManager(node.OwnerDocument.NameTable);

nsMgr.AddNamespace("sa", "http://example.com/");

XmlNode selected = node.SelectSingleNode("/sa:name1/sa:name2/sa:name3", nsMgr);
if (selected != null)
{
  ...
}

Instead of http://example.com/ you need of course to use the URI of the nodes in the input document, I think the namespace URI is http://rep.oio.dk/uvm.dk/studieadm/common/xml/schemas/2006/02/20/.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Hello Martin Im pretty sure I have to use this URI 'http://rep.oio.dk/uvm.dk/studieadm/common/xml/schemas/2006/02/20/SA_UndervisningOplysninger.xsd' But Now when I try to selectsingleNode I always get null ? – user2378036 Jun 12 '14 at 11:22
  • Try `http://rep.oio.dk/uvm.dk/studieadm/common/xml/schemas/2006/02/20/`, without the file name. – Martin Honnen Jun 12 '14 at 11:50
  • Thanks for the help but I just tried this and this change anything still only null. – user2378036 Jun 12 '14 at 11:55
  • Please edit your question and provide a sample of the XML input showing the namespaces declared in there. – Martin Honnen Jun 12 '14 at 11:58
  • found this post http://stackoverflow.com/questions/6950032/xmlnode-selectsinglenode-syntax-to-search-within-a-node-in-c-sharp I needed './' infront of my xpath to select from current node. but thanks for the rest this help me alot. – user2378036 Jun 12 '14 at 13:51
0

The error is clear: You need to add a namespace manager to your code for your xpath query to work. Use the overloaded version of SelectSingleNode() which accepts an instance of XmlNamespaceManager as argument.

  XmlDocument doc = new XmlDocument();
  doc.Load("booksort.xml");

  //Create an XmlNamespaceManager for resolving namespaces.
  XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
  nsmgr.AddNamespace("bk", "urn:samples");

  //Select the book node with the matching attribute value.
  XmlNode book;
  XmlElement root = doc.DocumentElement;
  book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);

  Console.WriteLine(book.OuterXml);

http://msdn.microsoft.com/en-us/library/h0hw012b(v=vs.110).aspx

Oscar
  • 13,594
  • 8
  • 47
  • 75
0

You need to add a namespace manager for the document before your call to SelectSingleNode:

XmlNamespaceManager xmlnsMan = new XmlNamespaceManager(xml.NameTable);
xmlnsMan.AddNamespace("sa", "[namespace]);
DGibbs
  • 14,316
  • 7
  • 44
  • 83