How to Select XML Nodes with XML Namespaces from an XmlDocument?
You have to declare the dc
namespace prefix using an XmlNamespaceManager before you can use it in XPath expressions:
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
for (int i = 0; i < 5; i++) {
XmlNode rssDetail = rssItems[i].SelectSingleNode("dc:creator", nsmgr);
if (rssDetail != null) {
user = rssDetail.InnerText;
} else {
user = "";
}
}
How to select a node with namespace from XmlDocument?
You need to add a XmlNamespaceManager
to be able to select the node:
XmlNode.SelectSingleNode Method (String, XmlNamespaceManager)
string xmlstr = "<string xmlns=\"http://example.com/proj1\">True|Success</string>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlstr);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("ab", "http://example.com/proj1");
XmlNode stringNode = xdoc.SelectSingleNode("//ab:string", nsmgr);
string message = stringNode.InnerText;
Besides your xml string is invalid in the example because it contains double quotes inside that are not escaped.
Simplest way to get XML nodes with namespace?
Namespace bindings are inherited, so the child elements are in the same namespace as their parents here.
You need to add the missing namespace prefixes to your query:
//anet:messages/anet:message/anet:text
That said, I'd usually prefer LINQ to XML over XPath:
XNamespace ns = "AnetApi/xml/v1/schema/AnetApiSchema.xsd";
var root = XElement.Parse(xml);
var text = (string) root.Elements(ns + "messages")
.Descendants(ns + "text")
.Single();
See this fiddle for a working demo.
Select Nodes with XPath when the XML document contains namespaces
You have two namespaces here. First is
http://schemas.datacontract.org/2004/07/Company.Product.Components.Model
Root element (ComponentSettings
), RemoteTracer
and everything below it belong to this namespace. Second namespace is
http://schemas.datacontract.org/2004/07/Company.Configuration
Created
, LastLoaded
and Saved
belong to it.
To get the node you need, you have to prefix all elements in your xpath query with their respective namespace prefixes. Mapping of those prefixes to actual namespaces you can do like this:
var componentConfigXmlDocument = new XmlDocument();
componentConfigXmlDocument.LoadXml(File.ReadAllText(@"G:\tmp\xml.txt"));
var ns = new XmlNamespaceManager(componentConfigXmlDocument.NameTable);
ns.AddNamespace("model", "http://schemas.datacontract.org/2004/07/Company.Product.Components.Model");
ns.AddNamespace("config", "http://schemas.datacontract.org/2004/07/Company.Configuration");
And then query like this:
var remoteTracers = componentConfigXmlDocument.SelectNodes("//model:RemoteTracer/model:TraceListener/model:Url", ns);
How to Select XML Nodes with XML Namespaces with C#
I am not sure what is special about your XML structure.
I would write the code little differently
string xmlNamespace = String.Empty;
XmlNamespaceManager nsmgr;
XmlNodeList nodeInfo = FABRequestXML.GetElementsByTagName("RootNodeName");
xmlNamespace = Convert.ToString(nodeInfo[0].Attributes["xmlns"].Value);
nsmgr = new XmlNamespaceManager(MyXml.NameTable);
nsmgr.AddNamespace("AB", xmlNamespace);
XmlNode myNode = MyXml.DocumentElement.SelectSingleNode("AB:NodeName", nsmgr);
Hope that helps
PowerShell XML select node with namespace
As @AnsgarWiechers stated, every node has to be prefixed by its namespace because there is no inheritance.
MWE
$StandaloneXML = "test.xml"
# Load XML content
$NewStandaloneXML = New-Object -TypeName "System.XML.XMLDocument"
$NewStandaloneXML.Load($StandaloneXML)
# Get namespace
$Namespace = New-Object -TypeName "Xml.XmlNamespaceManager" -ArgumentList $NewStandaloneXML.NameTable
$Namespace.AddNamespace("jboss", $NewStandaloneXML.DocumentElement.NamespaceURI)
$NewStandaloneXML.SelectNodes("jboss:server/jboss:interfaces/jboss:interface", $Namespace)
To make things easier, I have built a small function to automatically prefix each nodes in the XPath
provided.
function Select-XMLNode {
[CmdletBinding()]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "XML content"
)]
[ValidateNotNullOrEmpty()]
[System.XML.XMLDocument]
$XML,
[Parameter (
Position = 2,
Mandatory = $true,
HelpMessage = "XPath corresponding to the node"
)]
[ValidateNotNullOrEmpty()]
[String]
$XPath,
[Parameter (
Position = 3,
Mandatory = $false,
HelpMessage = "Namespace"
)]
[ValidateNotNullOrEmpty()]
[String]
$Namespace = $XML.DocumentElement.NamespaceURI
)
Begin {
# Variables
$Delimiter = "/"
$Alias = "x"
$SpecialCharacters = [RegEx]::New('^[/.@]*')
if ($XPath -match $SpecialCharacters) {
$Prefix = $Matches[0]
$XPath = $XPath -replace $SpecialCharacters, ''
}
}
Process {
# Get namespace
$NamespaceManager = New-Object -TypeName "Xml.XmlNamespaceManager" -ArgumentList $XML.NameTable
$NamespaceManager.AddNamespace($Alias, $Namespace)
# Split XPath to identify nodes
$Nodes = $XPath.Split($Delimiter)
$PrefixedNodes = New-Object -TypeName "System.Collections.ArrayList"
# Prefix nodes with namespace (alias)
foreach($Node in $Nodes) {
if ($Node) {
[Void]$PrefixedNodes.Add("${Alias}:${Node}")
}
}
# Join prefixed-nodes to create new XPath with namespace
$XPathWithNamespace = $PrefixedNodes -join $Delimiter
# Check XPath prefix
if ($Prefix) {
$XPathWithNamespace = $Prefix + "" + $XPathWithNamespace
}
# Select and return nodes
$SelectedNodes = $XML.SelectNodes($XPathWithNamespace, $NamespaceManager)
return $SelectedNodes
}
}
Finding specific node in xml document with namespace in C#
Issues with your code:
- to get namespace that used for given prefix (
w
in this case) you should either check properxmlns:w
attribute (not defaultxmlns
, nor some random one likexmlns:alm
) or in most cases simply specify explicitly - as it will never change in case of valid WordML documents for example. - to select elements staring from the root you either need to use XPath searching from the root (
/w:....
) or perform select on node that have root element as child (document itself).
Possible variants of working code:
xmlNamespace = Convert.ToString(nodeInfo[0].Attributes["xmlns:w"].Value);
nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("w", xmlNamespace);
XmlNode myNode = xml.SelectSingleNode("w:wordDocument/w:body", nsmgr);
or
nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml");
XmlNode myNode = xml.DocumentElement
.SelectSingleNode("/w:wordDocument/w:body", nsmgr);
or flat out ignore namespaces with local-name()
function:
XmlNode myNode = xml.SelectSingleNode(
"/*[local-name()='wordDocument']/*[local-name()='body']", nsmgr);
Cannot select an XML node with XML namespace using SelectSingleNode method
You should declare the namespace prefix using an XmlNamespaceManager
before you can use it in XPath expressions.
XmlDocument doc = new XmlDocument ();
doc.Load("/Users/buttercup/Projects/23564466/kram.xml");
XmlNamespaceManager nmspc = new XmlNamespaceManager(doc.NameTable);
nmspc.AddNamespace("form", "http://www.example.com/file.xsd");
nmspc.AddNamespace("addData", "http://www.example.com/file2.xsd");
nmspc.AddNamespace("_colored", "http://www.example.com/colored.xsd");
string fieldValue = doc.SelectSingleNode("/form:Documents/_colored:_colored/_colored/_Field1", nmspc).InnerText;
http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.aspx
Selecting node with several namespaces values in XML document returns nothing
In XPath, element name without prefix is always considered in empty namespace. In XML though, there is default namespace which elements implicitly inherits by default, this one in your particular XML :
xmlns="uuid:ebfd9-45-48-a9eb-42d"
I'd suggest to use a default prefix, say d
, in your XPath. And then map the prefix to the root element's namespace :
......
Dim nsManager As New XmlNamespaceManager(New NameTable())
nsManager.AddNamespace("d", m_xmld.DocumentElement.NamespaceURI)
test = doc.SelectSingleNode("d:Data/d:Info", nsManager)
The above will work on both cases (XML document with and without default namespace), but not in case an XML with default namespace declared locally at the descendant elements level.
Finding specific value from xml document with namespace in C#
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
StringReader sReader = new StringReader(xml);
XmlReader xReader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(xReader);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public Body Body { get; set; }
}
public class Body
{
[XmlArray(ElementName = "InitSessionRp", Namespace = "http://schemas.compassplus.com/two/1.0/fimi.xsd")]
[XmlArrayItem(ElementName = "Response", Namespace = "http://schemas.compassplus.com/two/1.0/fimi.xsd")]
public List<Response> Response { get; set; }
}
public class Response
{
[XmlElement(ElementName = "Id", Namespace = "http://schemas.compassplus.com/two/1.0/fimi_types.xsd")]
public string Id { get; set; }
[XmlElement(ElementName = "NeedCAPAuth", Namespace = "http://schemas.compassplus.com/two/1.0/fimi_types.xsd")]
public string NeedCAPAuth { get; set; }
[XmlAttribute()]
public string NextChallenge { get; set; }
[XmlAttribute(AttributeName = "Response")]
public string aResponse { get; set; }
[XmlAttribute()]
public string Ver { get; set; }
[XmlAttribute()]
public string Product { get; set; }
}
}
Related Topics
The Name "Xyz" Does Not Exist in the Namespace "Clr-Namespace:Abc"
Why Does Stylecop Recommend Prefixing Method or Property Calls with "This"
Am I Misunderstanding Linq to SQL .Asenumerable()
If Int32 Is Just an Alias for Int, How Can the Int32 Class Use an Int
Complex Type Is Getting Null in a APIcontroller Parameter
In .Net/C# Test If Process Has Administrative Privileges
Create PDF in Memory Instead of Physical File
How to Conditionally Compile My C# for Mono VS. Microsoft .Net
How to Read the Data in a Wav File to an Array
How Does Comparison Operator Works with Null Int
How to Perform a Cross Join with Linq to SQL
How Expensive Is the Lock Statement
Using Variables Inside Strings
Which Is the Correct C# Infinite Loop, for (;;) or While (True)