2

I have the following code

XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("//somePath/FieldData[@Location='Payer's Name']/@Value");

The single quote gives the exception System.Xml.XPath.XPathException. I have tried escaping it with a slash ('\'), ('), double single quotes. Nothing seems to work though. Any idea how to resolve this?

nnm
  • 1,335
  • 5
  • 18
  • 32

5 Answers5

1

There are basically two ways to reliably handle this situation.

The first way is to define a VariableContext and put the value you want to use in an XPath variable. Then you can use an expression like:

//somePath/FieldData[@Location = $user]/@Value

I describe how to do that in this post. It requires creating a VariableContext class as nothing suitable is built into .NET, but the example I provide should work as-is for most cases.

The other option is to use Linq-to-XML. If you go that route, you can query the node using Linq instead of XPath and delimiters are not an issue this way:

// here the doc variable is an XElement or XDocument
var value = (from fd in doc.Descendants("FieldData")
            where (string)fd.Attribute("Location") == sUser
            select fd.Attribute("Value")).FirstOrDefault();

or with the other Linq syntax:

var value = doc.Descendants("FieldData")
               .Where(fd => (string)fd.Attribute("Location") == sUser)
               .Select(fd => fd.Attribute("Value"))
               .FirstOrDefault();
Community
  • 1
  • 1
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

you cannot have a space in the xpath "[@Location='Player's Name'] take the space out

SuncoastOwner
  • 263
  • 2
  • 9
  • 1
    A space causes no problems.I have used it with a space before – nnm Dec 27 '14 at 15:34
  • A space in a string constant is not a syntactic element and will not make any difference. The issue is clearly the apostrophe inside a string delimited by apostrophes, causing the parse to believe the string constant ends earlier than intended, and being unable to parse the following string fragment as a valid XPath, which of course, it's not. – Flynn1179 Dec 27 '14 at 16:20
0

Note the single quote (trouble maker) has been replaced with a double quote.

string sUser = "Payer's name"; // as per OP requirement considering only single quote expr = nav.Compile("//somePath/FieldData[@Location=\"" + sUser + "\"]/@Value")

Avijit
  • 1,219
  • 2
  • 15
  • 28
  • So what if the variable contains a double quote instead? If you can guarantee this won't happen then this would work, but I'd definitely make sure it's documented that this is a precondition of this code fragment. – Flynn1179 Dec 27 '14 at 16:30
0

Is there any reason why you cannot use LINQ if you use language C#?

using System.Xml.Linq;

public class XmlParser
{
    public XDocument doc { get; set;}

    public XmlParser (XDocument doc)
    {
        this.doc = doc;
    }

    public List<XElement> searchElements (String elementName)
    {
        return doc.Elements(elementName).ToList<XElement>(); //this will search for all child nodes and return the elements with the specified name.
    }
}
Petr Kloza
  • 43
  • 7
0
expr = nav.Compile("//somePath/FieldData[@Location=\"Payer's Name\"]/@Value");
ewelina
  • 1
  • 1