Reading the xml below shows error in c#

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <w:document xmlns:ve="" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="" xmlns:m="" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="" xmlns:wne="">
      <w:customXml w:uri="Sample" w:element="note">
        <w:p w:rsidR="00B06944" w:rsidRDefault="0051608D" w:rsidP="000E0B9F">
          <w:customXml w:element="to">
            <w:r w:rsidR="000E0B9F" w:rsidRPr="00B84BAE">
              <w:t xml:space="preserve">Saran </w:t>

as I want to read the node < w:r > for this i am writing the below code

XmlDocument doc = new XmlDocument();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);
XmlNode node = doc.SelectSingleNode("/w:body/w:customXml/w:r", namespaceManager);

which gives:

The error shown in this line is Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

How can I read the Xml


You need to tell namespaceManager about the meaning of the alias w. It sounds like this is redundant (from the file), but it is not the case that the aliases you want for query are necessarily those from the source, since the meaning of the file is identical if I replace all the w aliases in the source document with foo (as long as I also define foo:xmlns to the be same). Or I could use xmlns instead of aliases throughout.


XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);
XmlNode node = doc.SelectSingleNode("/w:body/w:customXml/w:r", namespaceManager);

This allows your query to succeed identically, regardless of the specific aliases used in the source.

It doesn't give me that error - it gives me an error because the <w:p> tag doesn't have a closing tag, and neither does the outer <w:customXml>. Once those are fixed, the document loads correctly.

There's then the matter of getting the right query. Marc has addressed this in his answer, but personally I'd use the methods in LINQ to XML instead:

var doc = XDocument.Load("test.xml");
XNamespace w = "";
var element = doc.Root.Element(w + "body")
                      .Element(w + "customXml")
                      .Element(w + "p")
                      .Element(w + "customXml")
                      .Element(w + "r");

This finds the element correctly. Note that it's not the same as your original XPath even aside from the namespaces - look carefully at your XML; it doesn't have a <w:r> element inside the outer <w:customXml> element.

your xml is not valid. you have no closing w:p tag...

Need Your Help

Visual Studio alternative with very specific requirements

c++ visual-studio ide

I couldn't find an alternative to Visual Studio that is lightweight, as in requiring: &lt;500mb of disk space, ~256mb of memory, ~1.6Ghz CPU, has autocomplete, works under XP, doesn't require to in...

Using static class methods in multi-threaded programming

java multithreading thread-safety static-methods static-members

I am building a webcrawler which is using two classes: a downloader class and an analyzer class. Due to my design of the program I had some methods which I outsourced to a static class named utils

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.