Xml/Xslt - how to read XML node that conatins @ nad single quotes in attribute

Asked By Sachin Mishra on 10-May-12 12:15 PM
Earn up to 50 extra points for answering this tough question.

I have an XML which contains @ and single quotes in attribute value and when I want to get the value of that node which contains the @ and single quotes I got an error “Invalid token” finally I tried to escape single quotes but I did not any value it showed me nothing in SelectSingleNode()

Here is the sample XML which contains @ and single quotes in title node and I want to get those nodes that has XPATH = Test[@ABC='4'],please suggest me the syntax to select that node.

 

<? xml version="1.0" ?>

<Book>

<Author>ABC</ Author >

<title  val="Test[@ABC='4']">title1</title>

<title  val="Test[@ABC='3']">title2</title>

</Book>

Vikram Singh Saini replied to Sachin Mishra on 12-May-12 01:21 PM

The problem you were facing was due to use of single quotes in XPathExpression. And that is why it was throwing error as Invalid key token. Try below working code as it is. This code reads the attribute values of elements successfully.


01.using System;
02.using System.Xml;
03. 
04.namespace ReadXml
05.{
06.  class Program
07.  {
08.    static void Main(string[] args)
09.    {
10.      XmlDocument egg = new XmlDocument();
11.      egg.Load(@"Path to\test.xml");
12. 
13.      XmlNodeList eggElement = egg.SelectNodes("//Book/title");
14. 
15.      foreach (XmlElement element in eggElement)
16.      {
17.        string val = element.GetAttribute("val");
18.        string xPathExpression = "//title[@val=" + EncodeXpathString(val) + "]";
19. 
20.        XmlElement eggItem = (XmlElement)egg.SelectSingleNode(xPathExpression);
21.        Console.WriteLine("Found value of attribute val: "+eggItem.GetAttribute("val"));
22.        Console.ReadLine();
23.      }
24. 
25.    }
26. 
27.    // Function to handle single quotes that result in error as
28.    // System.Xml.XPath.XPathException: '//title[@val='Test[@ABC='4']']' has an invalid token.
29.    private static string EncodeXpathString(string args)
30.    {
31.      string result = "";
32.      if (args.Contains("'"))
33.      {
34.        string[] inputStrings = args.Split('\'');
35.        foreach (string input in inputStrings)
36.        {
37.          if (result != "")
38.            result += ",\"'\",";
39.          result += "\"" + input + "\"";
40.        }
41.        result = "concat(" + result + ")";
42.      }
43.      else
44.      {
45.        result = "'" + args + "'";
46.      }
47.      return result;
48.    }
49.  }
50.}

Let us know whether the solution helped you in achieving the objective.

LIJO PHILIP replied to Sachin Mishra on 18-May-12 12:33 AM

If you need read attribute node from xml you can use:

  • http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx class - easy to use - it's Linq to XML
  • http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx class
  • http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx class - "Represents a reader that provides fast, non-cached, forward-only access to XML data." (msdn)
LIJO PHILIP replied to Sachin Mishra on 18-May-12 12:39 AM
sorry for the above. links got messed up. try  this

If you need read attribute node from xml you can use:

XDocument class - easy to use - it's Linq to XML

Link: http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx  

XmlDocument class

Link: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx class


XmlReader class - "Represents a reader that provides fast, non-cached, forward-only access to XML data."
Link:http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
(msdn)