c# Xpath with multiple namespaces - c#

Can somebody explain me please why this is not working?
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Bing2_ss.xml");
//Instantiate an XmlNamespaceManager object.
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
//Add the namespaces used in feed to the XmlNamespaceManager.
//xmlnsManager.AddNamespace("base", "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Composite");
xmlnsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
xmlnsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices/");
xmlnsManager.AddNamespace(String.Empty, "http://www.w3.org/2005/Atom");
XmlNodeList nodeList;
XmlElement root = xmlDoc.DocumentElement;
nodeList = root.SelectNodes("//m:properties/d:WebTotal", xmlnsManager);
foreach (XmlNode nd in nodeList)
{
System.Console.WriteLine(nd.InnerText);
}
This is the original xml: http://gezond-afslanken.info/bing2_ss.xml

You have an extra trailing slash on one of your namespaces. Try
xmlnsManager.AddNamespace("d","http://schemas.microsoft.com/ado/2007/08/dataservices");

Related

C# select node from XML

I have this code in C#, I need to get a Cube nodes, but my list of nodes is empty..
string url = #"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");
XmlNodeList nodes = xmlDoc.SelectNodes("/gesmes:Envelope/Cube", manager);
What am I doing wrong?
There is a default namespace http://www.ecb.int/vocabulary/2002-08-01/eurofxref which you will need to register, to access the Cube element.
Otherwise, the XPath expression tries to find an un-namespaced Cube element, which doesn't exist in the document. XPath has no concept of a default namespace.
string url = #"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");
manager.AddNamespace("default", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
XmlNodeList nodes = xmlDoc.SelectNodes("/gesmes:Envelope/default:Cube", manager);
this will access the Cube child directly under the gesmes:Envelope. Depending what you want to achieve, you may want to use one of the following XPath expressions instead:
/gesmes:Envelope/default:Cube/default:Cube/default:Cube
/gesmes:Envelope//default:Cube[#currency]
/gesmes:Envelope//default:Cube[#time]/default:Cube

Replace xml document parameters using c#

I want to replace following #eleval2,#eleval4 parameters with some other values using c#.net.Please help me to do it.
<root>
<element1>
<element2>
#eleval2
</element2>
</element1>
<element3>
<element4>
<element4>
#eleval4
</element4>
</element4>
</element3>
Directly updating node:
XmlDocument xml = new XmlDocument();
xml.Load("file_name.xml");
xml.SelectSingleNode("/root/element1/element2").InnerText = "NewValue";
For looping:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("path_here");
XmlNodeList tagNodes= xmlDoc.GetElementsByTagName("tag_of_your_interest");
//Loop through first child of above list
foreach (XmlNode chapter in tagNodes[0].ChildNodes)
{
//Perform your updates here
}
Load from string:
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
Get all list of nodes matches path:
XmlNodeList xnList = doc.SelectNodes("/sections/notebooks/article");

How to use XmlNamespaceManager and SelectSingleNode correctly?

I need to get node value from xml. The xml has namespace.
I have the following code
string xml =
"<file xmlns=\"SFAKT\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<document>test</document>" +
"</file>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDocument.NameTable);
ns.AddNamespace("sf", "SFAKT");
XmlNode node = xmlDocument.SelectSingleNode("sf:file/document");
But node = null
Can you tell me where is the error in my code?
You need to use the overloaded SelectSingleNode method and pass in the XmlNamespaceManager. Also, you need the sf prefix for the document node.
Pull the node out like this:
XmlNode node = xmlDocument.SelectSingleNode("sf:file/sf:document", ns);

How to read a node in an xml in this format

I have an getting an xml response on this format
<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n
<PlatformResponse xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://platform.intuit.com/api/v1\">\r\n
<ErrorMessage>OAuth Token rejected</ErrorMessage>\r\n
<ErrorCode>270</ErrorCode>\r\n
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>\r\n
</PlatformResponse>
I need to grab the value in the <ErrorCode> node, for that I did the following but it is not getting any values..
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlResponse);
XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
foreach (XmlNode xn in xnList)
{
result.Message = xn["ErrorCode"].InnerText;
}
Any help would be much appreciated.
There seems to be some dirt in your PlatformResponse node that's giving problems, ( xmlns:xsd= etc... )
Using this xml
String sXml = #"<?xml version='1.0' encoding='utf-8'?>
<PlatformResponse >
<ErrorMessage>OAuth Token rejected</ErrorMessage>
<ErrorCode>270</ErrorCode>
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>
</PlatformResponse>";
And select like
XmlNodeList xnList = xml.SelectNodes("/PlatformResponse");
Your code works fine.
I just tested the code and it does work fine:
XmlDocument xml = new XmlDocument();
XmlTextReader reader = new XmlTextReader("Path_to_your_xml");
xml.Load(reader);
XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
foreach (XmlNode xn in xnList)
{
MessageBox.Show(xn["ErrorCode"].InnerText);
}
For this, since the attribute is on the main document element itself, you can simply do
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlText);
result.Message = xml.DocumentElement["ErrorCode"].InnerText

XML - how to use namespace prefixes

I have this XML at http://localhost/file.xml:
<?xml version="1.0" encoding="utf-8"?>
<val:Root xmlns:val="http://www.hw-group.com/XMLSchema/ste/values.xsd">
<Agent>
<Version>2.0.3</Version>
<XmlVer>1.01</XmlVer>
<DeviceName>HWg-STE</DeviceName>
<Model>33</Model>
<vendor_id>0</vendor_id>
<MAC>00:0A:DA:01:DA:DA</MAC>
<IP>192.168.1.1</IP>
<MASK>255.255.255.0</MASK>
<sys_name>HWg-STE</sys_name>
<sys_location/>
<sys_contact>
HWg-STE:For more information try http://www.hw-group.com
</sys_contact>
</Agent>
<SenSet>
<Entry>
<ID>215</ID>
<Name>Home</Name>
<Units>C</Units>
<Value>27.7</Value>
<Min>10.0</Min>
<Max>40.0</Max>
<Hyst>0.0</Hyst>
<EmailSMS>1</EmailSMS>
<State>1</State>
</Entry>
</SenSet>
</val:Root>
I am trying to read this from my c# code:
static void Main(string[] args)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("http://localhost/file.xml");
XmlElement root = xmlDoc.DocumentElement;
// Create an XmlNamespaceManager to resolve the default namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("val", "http://www.hw-group.com/XMLSchema/ste/values.xsd");
XmlNodeList nodes = root.SelectNodes("/val:SenSet/val:Entry");
foreach (XmlNode node in nodes)
{
string name = node["Name"].InnerText;
string value = node["Value"].InnerText;
Console.Write("name\t{0}\value\t{1}", name, value);
}
Console.ReadKey();
}
}
Problem is that the node is empty. I understand this is a common newbie problem when reading XML, still not able to solve what I am doing wrong, probably something with the Namespace "val" ?
You need to pass the namespace manager into the SelectNodes()
method.
Edit: corrected code
XmlNodeList nodes = root.SelectNodes("/val:Root/SenSet/Entry", nsmgr);
Just change you Xpath to:
XmlNodeList nodes1 = root.SelectNodes("/val:Root/SenSet/Entry",nsmgr);
Or:
XmlNodeList nodes = root.SelectNodes("SenSet/Entry");
Your xpath query string should be:
XmlNodeList nodes = root.SelectNodes("/val:Root/SenSet/Entry", nsmgr);
or more concisely,
XmlNodeList nodes = root.SelectNodes("//SenSet/Entry", nsmgr);

Categories

Resources