0

I have a XML document which looks like :

<stReq xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <EvalRes>
    <RenDtSrc xmlns="http://fakeurl.com/somthing/facade/params">
          <ContentType>application/pdf</ContentType>
          <DocumentName>Name</DocumentName>
          <Content>Doc Content</Content>
    </RenDtSrc>
  </EvalRes>
</stReq>

From a asp.net application, I am trying to check if a node <RenDtSrc> exists in a document or not. Following is the code I am using to read XML file and node element

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\Test\\Doc1.xml");
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/stReq/EvalRes/RenDtSrc");

Count of nodeList returns zero even though there are child nodes inside it. I think its something to do with namespace manager but I can't figure it out. Any help would be appreciated.

2 Answers 2

1
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\Test\\Doc1.xml");
var nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("s", "http://fakeurl.com/somthing/facade/params");
XmlNodeList nodeList = xmlDoc.SelectNodes("//s:RenDtSrc", nsm);
Sign up to request clarification or add additional context in comments.

1 Comment

Please refrain from posting raw code and give some explanation as to why this works.
0
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("//stReq/EvalRes/node()/*");

Output:

Node [0] = {Element, Name="ContentType"}
Node [1] = {Element, Name="DocumentName"}
Node [2] = {Element, Name="Content"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.