Iam trying to get a particular node with value equals my input parameter,my xpath is like this where b is the node I need
string xpath = "/Batches/Measurement/Batch[market=someval]/b";
<?xml version="1.0" encoding="utf-8" ?>
<Batches>
<Measurement>
<Batch>
<market>someval</market>
<b>someval</b>
</Batch>
</Measurement>
</Batches>
var xmlNode = xmlDoc.SelectNodes(xpath);
no nodes retruned always count 0 ,I checked that the xmldoc is loaded properly.
Your xpath is nearly perfect. Only keep in mind const values have to be put in apostrophe:
"/Batches/Measurement/Batch[market='someval']/b"
Update: C# code example:
XmlNodeList nodeList;
nodeList = root.SelectNodes("/Batches/Measurement/Batch[market='someval']/b");
foreach (XmlNode node in nodeList)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
Console.WriteLine(node.ChildNodes[i].InnerText);
}
}
The return value of SelectNodes is a nodeList. You have to iterate through it.
And a little bit shorter:
XmlElement root = doc.DocumentElement;
string text;
text = root.SelectSingleNode("/Batches/Measurement/Batch[market='someval']/b").InnerText;
Console.WriteLine(text);
Have you thought about using LINQ to XML?
It is slightly more efficient and shorter clearner syntax for selecting. I know you asked about Xpath so feel free to ignore this. Just making you aware of the option
var doc = XDocument.Load("c:\\tmp\\test.xml");
var result = doc.Descendants().Where(x => x.Element("b") != null)
.Select(x => x.Element("b").Value);
Related
How can I read an XML attribute using C#'s XmlDocument?
I have an XML file which looks somewhat like this:
<?xml version="1.0" encoding="utf-8" ?>
<MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream">
<Other stuff />
</MyConfiguration>
How would I read the XML attributes SuperNumber and SuperString?
Currently I'm using XmlDocument, and I get the values in between using XmlDocument's GetElementsByTagName() and that works really well. I just can't figure out how to get the attributes?
XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["SuperString"].Value;
}
You should look into XPath. Once you start using it, you'll find its a lot more efficient and easier to code than iterating through lists. It also lets you directly get the things you want.
Then the code would be something similar to
string attrVal = doc.SelectSingleNode("/MyConfiguration/#SuperNumber").Value;
Note that XPath 3.0 became a W3C Recommendation on April 8, 2014.
You can migrate to XDocument instead of XmlDocument and then use Linq if you prefer that syntax. Something like:
var q = (from myConfig in xDoc.Elements("MyConfiguration")
select myConfig.Attribute("SuperString").Value)
.First();
I have an Xml File books.xml
<ParameterDBConfig>
<ID Definition="1" />
</ParameterDBConfig>
Program:
XmlDocument doc = new XmlDocument();
doc.Load("D:/siva/books.xml");
XmlNodeList elemList = doc.GetElementsByTagName("ID");
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["Definition"].Value;
}
Now, attrVal has the value of ID.
XmlDocument.Attributes perhaps? (Which has a method GetNamedItem that will presumably do what you want, although I've always just iterated the attribute collection)
Assuming your example document is in the string variable doc
> XDocument.Parse(doc).Root.Attribute("SuperNumber")
1
If your XML contains namespaces, then you can do the following in order to obtain the value of an attribute:
var xmlDoc = new XmlDocument();
// content is your XML as string
xmlDoc.LoadXml(content);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
// make sure the namespace identifier, URN in this case, matches what you have in your XML
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol");
// get the value of Destination attribute from within the Response node with a prefix who's identifier is "urn:oasis:names:tc:SAML:2.0:protocol" using XPath
var str = xmlDoc.SelectSingleNode("/ns:Response/#Destination", nsmgr);
if (str != null)
{
Console.WriteLine(str.Value);
}
More on XML namespaces here and here.
I have an xml file as below
<Games>
<Game>
<name>Tzoker</name>
<file>tzoker1</file>
</Game>
<Game>
<file>lotto770</file>
</Game>
<Game>
<name>Proto</name>
<file>proto220</file>
</Game>
</Games>
I want to get the values of name and file items for every Game node.
It is easy by using this query.
string query = String.Format("//Games/Game");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn["name"].InnerText;
s2 = xn["file"].InnerText;
}
The problem is that there are some nodes that they don't have the name item. So the code above doesn't work.
I have solved the problem by using the following code
string query = String.Format("//Games/Game/name");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn.InnerText;
string query1 = String.Format("//Games/Game[name='{0}']/file", s1);
XmlNodeList elements2 = xml.SelectNodes(query1);
foreach (XmlNode xn2 in elements2)
{
s2 = xn2.InnerText;
}
}
The problem is that there is a case that two or more nodes have the same name value. So, the s2 variable will get the file value of the last node that the loop finds. So, I would like to find a way to get the sibling file value of the current name item. How could I do it? I try do move to the parent node of the current node and then to move to the file item but without success by using the following code.
string query = String.Format("//Games/Game/name");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn.InnerText;
string query1 = String.Format("../file");
XmlNodeList elements2 = xml.SelectNodes(query1);
foreach (XmlNode xn2 in elements2)
{
s2 = xn2.InnerText;
}
}
I hope there is a solution.
You can use Game[name] to filter Game elements to those with child element name. This is possible because child:: is the default axes which will be implied when no explicit axes mentioned. Extending this further to check for child element file as well, would be as simple as Game[name and file] :
string query = String.Format("//Games/Game[name]");
XmlNodeList elements1 = xml.SelectNodes(query);
foreach (XmlNode xn in elements1)
{
s1 = xn["name"].InnerText;
s2 = xn["file"].InnerText;
}
Now to answer your question literally, you can use following-sibling:: axes to get sibling element that follows current context element. So, given the context element is name, you can do following-sibling::file to return the sibling file element.
Your attempt which uses ../file should also work. The only problem was, that your code executes that XPath on xml, the XmlDocument, instead of executing it on current name element :
XmlNodeList elements2 = xn.SelectNodes("../file");
If I understand you correctly you want to find all games that have a name. You can do that using XPath. Here is a solution that uses LINQ to XML. I find that easier to work with than XmlDocument:
var xDocument = XDocument.Parse(xml);
var games = xDocument
.Root
.XPathSelectElements("Game[child::name]")
.Select(
gameElement => new {
Name = gameElement.Element("name").Value,
File = gameElement.Element("file").Value
}
);
The XPath to select all <Game> elements that have a <name> child element is Game[child::name].
I need to take in XML with common parent nodes but with varying child nodes. Once I get it, I need to grab the tag names of the child nodes and use those names as headers. In the following example, all incoming XML will be wrapped as follows:
<customers>
<customer>
...varying child nodes that do not have child nodes themselves
</customer>
</customers>
I have found that this works:
List<string> headerList = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(someXML);
XmlNodeList xnl = xmlDoc.SelectNodes("customers/customer");
foreach (XmlNode xn in xnl)
{
for (int x = 0; x < xn.ChildNodes.Count; x++)
{
headerList.Add(xn.ChildNodes[x].Name.ToString());
}
}
Is there a better way to do this?
Thanks in advance.
This should do the trick;
XDocument doc = XDocument.Load(someXML);
var headerList = doc.Descendants("customer").Elements().Select(x => x.Name);
Not necessarily "better", but it's a bit more concise I guess.
I am at my wits end on this one. Here's the document I have:
<?xml version="1.0"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Property1>TestObjectVal1</Property1>
<Property2>TestObjectVal2</Property2>
<Property3>TestObjectVal3</Property3>
<SubObject>
<Prop1>TestObject2Val1</Prop1>
<Prop2>TestObject2Val2</Prop2>
<Prop3>TestObject2Val3</Prop3>
</SubObject>
</TestObject>
I'm trying to copy select portions of it to an new XmlDocument object based on some specified XPaths. I've tried every permutation I can think of. Here's where I'm at now.
var filters = new[] { "Property1", "Property2", "SubObject/Prop1" };
var xmlDoc = GetObjectXml(obj); //Loads the document
var newDoc = (XmlDocument)xmlDoc.Clone();
newDoc.DocumentElement.RemoveAll();
var rootNode = xmlDoc.DocumentElement;
foreach (var filter in filters)
{
var nodes = rootNode.SelectNodes(filter);
foreach (XmlNode node in nodes)
{
var newNode = newDoc.ImportNode(node, true);
newDoc.DocumentElement.AppendChild(newNode);
}
}
What I'm getting back is this:
<?xml version="1.0"?>
<TestObject>
<Property1>TestObjectVal1</Property1>
<Property2>TestObjectVal2</Property2>
<Prop1>TestObject2Val1</Prop1>
</TestObject>
But I want this:
<?xml version="1.0"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Property1>TestObjectVal1</Property1>
<Property2>TestObjectVal2</Property2>
<SubObject>
<Prop1>TestObject2Val1</Prop1>
</SubObject>
</TestObject>
Any idea what I'm doing wrong?
It looks like the problem is that the xpath method is selecting the inner element, so that the *SubObject" information is getting lost. This code should result in the correct output for your specific example.
foreach (XmlNode node in nodes)
{
XmlElement newNode;
string[] xpathElements = filter.Split('/');
if (xpathElement.Length == 2)
{
newNode = newDoc.CreateElement(filter);
newNode.AppendChild(newDoc.ImportNode(node, true));
}
else
{
newNode = newDoc.ImportNode(node, true);
}
newDoc.DocumentElement.AppendChild(newNode);
}
Note that this code makes restrictive assumptions about what form the "filter" xpath expression must take... namely, it must be of the form "RootElement" or "RootElement/ChildElement" (no attributes an max depth of three). That might be enough depending on your use case, but solving for more general cases would be a bit more tricky ...
I have been trying to read an xml file. I have to extract value of nodes "Date" and "Name", but the problem is, they might appear at any level in XML hierarchy.
So when I try with this code,
XmlDocument doc = new XmlDocument();
doc.Load("test1.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//*");
string date;
string name;
foreach (XmlNode node in nodes)
{
date = node["date"].InnerText;
name = node["name"].InnerText;
}
and the XML file is ::
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>
<name>Aravind</name>
<date>12/03/2000</date>
</child>
</root>
the above code errors out, as <name> and <date> are not immediate child Elements of root.
is it possible to assume that parent/root nodes are unknown and just with the name of the nodes, copy the values ??
Depending on the exception you are getting, this may or may not be the exact solution. However, I would definitely check that date and name exist before doing a .InnerText on them.
foreach (XmlNode node in nodes)
{
dateNode = node["date"];
if(dateNode != null)
date = dateNode.InnerText;
// etc.
}
I would read up on XPATH and XPATH for C# to do this more efficiently
http://support.microsoft.com/kb/308333
http://www.w3schools.com/XPath/xpath_syntax.asp
Here's a little method that should allow you to get the innerText easily.
function string GetElementText(string xml, string node)
{
XPathDocument doc = new XPathDocument(xml);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile("//" + node);
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext())
{
// return 1st but there could be more
return iterator.Current.Value;
}
}
Try to use LINQ:
string xml = #"<?xml version='1.0' encoding='utf-8'?>
<root>
<date>12/03/2001</date>
<child>
<name>Aravind</name>
<date>12/03/2000</date>
</child>
<name>AS-CII</name>
</root>";
XDocument doc = XDocument.Parse(xml);
foreach (var date in doc.Descendants("date"))
{
Console.WriteLine(date.Value);
}
foreach (var date in doc.Descendants("name"))
{
Console.WriteLine(date.Value);
}
Console.ReadLine();
The Descendants method allows you to get all the elements that have a specified name.