473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML AND apostrophe

Siu
Hi,
I need to make some search in a XML file to get element and/or attributes of
element from my ASP.NET web application (in C#)

My problem is that the value that I search can have an a postrophe and that
could generate an error. Here a sempliefied code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILENAME);
XmlNode node =
xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.='"+strComment+"']");

The variable strComment can contain an apostrophe and that generates an
error. How can I avoid that?

Please help me, thank you.
Nov 12 '05 #1
8 6911


Siu wrote:

My problem is that the value that I search can have an a postrophe and that
could generate an error. Here a sempliefied code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILENAME);
XmlNode node =
xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.='"+strComment+"']");

The variable strComment can contain an apostrophe and that generates an
error. How can I avoid that?


xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.=\"" +
strComment + "\"]")
should do.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Siu
Thank you Martin, it works.

"Martin Honnen" wrote:


Siu wrote:

My problem is that the value that I search can have an a postrophe and that
could generate an error. Here a sempliefied code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILENAME);
XmlNode node =
xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.='"+strComment+"']");

The variable strComment can contain an apostrophe and that generates an
error. How can I avoid that?


xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.=\"" +
strComment + "\"]")
should do.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3
Hi,

I am having a similar problem to above but the suggested solution does not
fixe the issue.

I am using the following code to calculate a value:
objNavigator.Evaluate("sum(//ALLCASHISSUEDDETAIL[ACIDSTANDARDCURRENCYID = '"
+ strCurrencyID + "' and ACIDSECURITYNAME = \"" + strShareClass +
"\"]/ACIDAMOUNTISSUED)")

When strShareClass has an apostrope I receive the following error:
System.Xml.XPath.XPathException:
'sum(//NOTALLCASHISSUEDDETAIL[NACIDSTANDARDCURRENCYID = 'EUR' and
NACIDSECURITYNAME = "Ordinary&", Euro 1.27"]/NACIDAMOUNTISSUED)' has an
invalid token.

I have tried replacing the "\" with "apos:" but the xpath does not extract
the correct value then?

Lastbuilders

"Siu" wrote:
Thank you Martin, it works.

"Martin Honnen" wrote:


Siu wrote:

My problem is that the value that I search can have an a postrophe and that
could generate an error. Here a sempliefied code:
>
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILENAME);
XmlNode node =
xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.='"+strComment+"']");
>
The variable strComment can contain an apostrophe and that generates an
error. How can I avoid that?
xmlDoc.SelectSingleNode("/REGGROUPS/CLIENT/REGPATCH/COMMENT[.=\"" +
strComment + "\"]")
should do.

--

Martin Honnen
http://JavaScript.FAQTs.com/
May 28 '07 #4
* Baz wrote in microsoft.public.dotnet.xml:
>I am having a similar problem to above but the suggested solution does not
fixe the issue.

I am using the following code to calculate a value:
objNavigator.Evaluate("sum(//ALLCASHISSUEDDETAIL[ACIDSTANDARDCURRENCYID = '"
+ strCurrencyID + "' and ACIDSECURITYNAME = \"" + strShareClass +
"\"]/ACIDAMOUNTISSUED)")

When strShareClass has an apostrope I receive the following error:
System.Xml.XPath.XPathException:
'sum(//NOTALLCASHISSUEDDETAIL[NACIDSTANDARDCURRENCYID = 'EUR' and
NACIDSECURITYNAME = "Ordinary&", Euro 1.27"]/NACIDAMOUNTISSUED)' has an
invalid token.

I have tried replacing the "\" with "apos:" but the xpath does not extract
the correct value then?
You cannot use ' in the .Evaluate(...) method, it does not know any
such character references. In XPath, you have to enclose string literals
in either a pair of 's or a pair of "s. In your example above you use "s
so strShareClass must not contain another ". There are five ways around
the problem:

1. If you know strShareClass will never contain ', use ' instead.

2. If you know strShareClass will never contain both ' and " at the
same time, pick the delimiter accordingly when building the ex-
pression.

3. Declare a variable like $quot and replace all occurences of "
in strShareClass by $quot; I am not sure how to do that in .NET,
it might not be possible.

4. Build the string inside the expression using concat(...). As an
example, if you have a string <<< foo " ' bar >>>, the expression
might look like ... = concat("foo ", '"', " ' bar ");

5. Add the searched for string to a special attribute, element, etc.
in the document and specify that in the expression, for example,
... = /root/@tmp-strShareClass-search-string; this is the worst of
the options.

If 3. is not possible in .NET, I recommend to write a function for 4.
and use it whenever you build XPath expressions using user-defined
values.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
May 28 '07 #5
Thanks for the suggestions Bjoern.

As the user can enter any characters options one and two would not suit.
Option 4 would get very inefficient if users entered multiple quotes so I am
going to look into option 3 though it may not be supported.

My preferred option would be to use a custom context and override the sum
function and pass in the variables escaping as appropriate but I have not
found any pointers on how to implement this on MSDN.

My other solution would be to remove the strShareClass from the select and
only limit by Currency which will never have quotations or apostrophes.

Baz

"Bjoern Hoehrmann" wrote:
* Baz wrote in microsoft.public.dotnet.xml:
I am having a similar problem to above but the suggested solution does not
fixe the issue.

I am using the following code to calculate a value:
objNavigator.Evaluate("sum(//ALLCASHISSUEDDETAIL[ACIDSTANDARDCURRENCYID = '"
+ strCurrencyID + "' and ACIDSECURITYNAME = \"" + strShareClass +
"\"]/ACIDAMOUNTISSUED)")

When strShareClass has an apostrope I receive the following error:
System.Xml.XPath.XPathException:
'sum(//NOTALLCASHISSUEDDETAIL[NACIDSTANDARDCURRENCYID = 'EUR' and
NACIDSECURITYNAME = "Ordinary&", Euro 1.27"]/NACIDAMOUNTISSUED)' has an
invalid token.

I have tried replacing the "\" with "apos:" but the xpath does not extract
the correct value then?

You cannot use &apos; in the .Evaluate(...) method, it does not know any
such character references. In XPath, you have to enclose string literals
in either a pair of 's or a pair of "s. In your example above you use "s
so strShareClass must not contain another ". There are five ways around
the problem:

1. If you know strShareClass will never contain ', use ' instead.

2. If you know strShareClass will never contain both ' and " at the
same time, pick the delimiter accordingly when building the ex-
pression.

3. Declare a variable like $quot and replace all occurences of "
in strShareClass by $quot; I am not sure how to do that in .NET,
it might not be possible.

4. Build the string inside the expression using concat(...). As an
example, if you have a string <<< foo " ' bar >>>, the expression
might look like ... = concat("foo ", '"', " ' bar ");

5. Add the searched for string to a special attribute, element, etc.
in the document and specify that in the expression, for example,
... = /root/@tmp-strShareClass-search-string; this is the worst of
the options.

If 3. is not possible in .NET, I recommend to write a function for 4.
and use it whenever you build XPath expressions using user-defined
values.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
May 28 '07 #6
Baz wrote:
My preferred option would be to use a custom context and override the sum
function and pass in the variables escaping as appropriate but I have not
found any pointers on how to implement this on MSDN.
This <http://msdn.microsoft.com/msdnmag/issues/03/02/XMLFiles/article
has an example of implementing IXsltContextFunction and XsltContext.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 28 '07 #7
* Baz wrote in microsoft.public.dotnet.xml:
>As the user can enter any characters options one and two would not suit.
Option 4 would get very inefficient if users entered multiple quotes so I am
going to look into option 3 though it may not be supported.
I don't think there is anything inefficient about #4. The expression
might become a little bit longer, but parsing it evaluating the concat
function is something a modern computer can do millions of times per
second. It seems http://support.microsoft.com/kb/324462 you can do #3
though.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
May 28 '07 #8
Hi Bjoern\Martin,

Thanks for the Pointers.

I wrote a sumfunction similar to that shown on MSDN. Here is the code in
case anyone finds it useful. No warranties gaurantees etc.

private class SumFunction : IXsltContextFunction
{
public const string Namespace = "CRO";
public const string FunctionName = "sum";

private System.Xml.XPath.XPathResultType[] m_argTypes =
new System.Xml.XPath.XPathResultType[] {
System.Xml.XPath.XPathResultType.Number};

#region IXsltContextFunction Members

public System.Xml.XPath.XPathResultType[] ArgTypes
{
get
{
return m_argTypes;
}
}

private double GetNumberFromInvokeArgument(object arg)
{
double dblValue = 0;
double dblOut = 0;
if (arg is XPathNodeIterator)
{
XPathNodeIterator i = (XPathNodeIterator)arg;
XPathNavigator navigator = i.Current;

while (i.MoveNext())
{
if(double.TryParse(i.Current.Value, out dblOut))
{
dblValue += dblOut;
}
else
{
throw new ArgumentException("sum() function
only supports number values");
}
}
}
else
{
throw new ArgumentException("sum() only supports node
path");
}

return dblValue;
}

public object Invoke(XsltContext xsltContext, object[] args,
System.Xml.XPath.XPathNavigator docContext)
{
if (args.Length == 0)
{
throw new ArgumentException("sum() needs at least one
argument ");
}

double argument1 = GetNumberFromInvokeArgument(args[0]);

return argument1;
}

public int Maxargs
{
get { return 1; }
}

public int Minargs
{
get { return 1; }
}

public System.Xml.XPath.XPathResultType ReturnType
{
get { return System.Xml.XPath.XPathResultType.Number; }
}

#endregion
}

Herer is the calling code

XPathExpression expr1 =
objNavigator.Compile("CRO:sum(//NOTALLCASHISSUEDDETAIL[NACIDSTANDARDCURRENCYID = $CurrencyId and NACIDSECURITYNAME = $SecurityName]/NACIDAMOUNTISSUED)");
XPathExpression expr2 =
objNavigator.Compile("CRO:sum(//ALLCASHISSUEDDETAIL[ACIDSTANDARDCURRENCYID =
$CurrencyId and ACIDSECURITYNAME = $SecurityName]/ACIDAMOUNTISSUED)");
CustomContext xsltContextAuthorisedShareClass = new
CustomContext((NameTable)objXML.NameTable);
xsltContextAuthorisedShareClass.AddParam("Security Name",
strShareClass);
xsltContextAuthorisedShareClass.AddParam("Currency Id",
strCurrencyID);
xsltContextAuthorisedShareClass.AddParam("ParValue ",
dblParValue.ToString());
xsltContextAuthorisedShareClass.AddNamespace("GCM" ,
"GCM");
expr1.SetContext(xsltContextAuthorisedShareClass);
expr2.SetContext(xsltContextAuthorisedShareClass);
dblAmt2 = (double)objNavigator.Evaluate(expr1);
dblAmt3 = (double)objNavigator.Evaluate(expr2);

Baz

"Bjoern Hoehrmann" wrote:
* Baz wrote in microsoft.public.dotnet.xml:
As the user can enter any characters options one and two would not suit.
Option 4 would get very inefficient if users entered multiple quotes so I am
going to look into option 3 though it may not be supported.

I don't think there is anything inefficient about #4. The expression
might become a little bit longer, but parsing it evaluating the concat
function is something a modern computer can do millions of times per
second. It seems http://support.microsoft.com/kb/324462 you can do #3
though.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
May 28 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Meyer1228 | last post by:
You all helped me with function keys last week. Thanks so much; your advice was right on. My question now - does anyone have a function that I can use to test my sql statements for the...
2
by: Michael Sterling | last post by:
i'm using delphi 7 and have a query in which i'm trying to find names that have an apostrophe in them, i.e. "o'mally". my problem is that when i write my select statement i can't get the quotes...
2
by: InvisibleMan | last post by:
Thank in advance for your help... I have the following javascript: document.write(' <a class="whitenav" href="javascript:window.external.AddFavorite ('http://www.mydomain.com','Welcome to...
13
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices...
1
by: congngo | last post by:
Hi all Every time I export a table into an excel spreadsheet. It has a leading apostrophe on every cells. This drive me nut. I have to do a work around by export table into a txt file than...
1
by: spacehopper_man | last post by:
hi - I am having "apostrophe in sql" problems ;) I am executing a stored procedure on SQL Server - and passing in a string parameter. the string has a single apostrophe in it. the call...
1
by: Rose | last post by:
Hi all, I'm trying to create a clickable link, but the pesky apostrophe is preventing the link from getting displayed properly. I'm displaying the contents of a folder (with contains the...
2
by: Tom | last post by:
Hi, I have some kind of problems with an apostrophe character ('). I would like to select from DataTable DataRow containing value horses' (with an apostrophe on the end). But when I do it in an...
9
by: Thomas 'PointedEars' Lahn | last post by:
Jukka K. Korpela wrote: IBTD. For example, in English it is customary (and AIUI expected) to use the character that ’ represents should be used to delimit a quotation within direct speech...
4
by: Razzbar | last post by:
I'm working on a bookmarklet that grabs information from a page and submits it to a server. Yet another social bookmarking application. I'm having trouble with page titles that include an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.