Xpath error has an Invalid Token - c#

I have the following C# code:
var selectNode = xmlDoc.SelectSingleNode("//CodeType[#name='" + codetype +
"']/Section[#title='" + section + "']/Code[#code='" + code + "' and
#description='" + codedesc + "']") as XmlElement;
when I run my code it raises the error saying "the above statement has an invalid token"
These are the values for the above statement.
codeType=cbc
section="Mental"
codedesc="Injection, enzyme (eg, collagenase), palmar fascial cord (ie,
Dupuytren's contracture"

Notice the apostrophe (') in codedesc?
You need to escape it somehow. The XPath interpreter considers it as a string delimiter and doesn't know how to treat the other apostrophe after it.
One way you can do that is by enclosing your string in double quotes instead of apostrophes.
Your code could therefore become:
var selectNode = xmlDoc.SelectSingleNode(
"//CodeType[#name='" + codetype + "']" +
"/Section[#title='" + section + "']" +
"/Code[#code=\"" + code + "' and #description='" + codedesc + "\"]")
as XmlElement;
(note that on the fourth line, the apostrophes(') became double quotes(\"))
While this approach works for the data you presented, you are still not 100% safe: other records could contain double quotes themselves. If that happens, we'll need to think of something for that case as well.

You can get selected node based on index , if any special characters in the xml schema. So , here looks below implementation for delete selected index node from xml schema.
XML SelectSingleNode Delete Operation
var schemaDocument = new XmlDocument();
schemaDocument.LoadXml(codesXML);
var xmlNameSpaceManager = new XmlNamespaceManager(schemaDocument.NameTable);
if (schemaDocument.DocumentElement != null)
xmlNameSpaceManager.AddNamespace("x", schemaDocument.DocumentElement.NamespaceURI);
var codesNode = schemaDocument.SelectSingleNode(#"/x:integration-engine-codes/x:code-categories/x:code-category/x:codes", xmlNameSpaceManager);
var codeNode = codesNode.ChildNodes.Item(Convert.ToInt32(index) - 1);
if (codeNode == null || codeNode.ParentNode == null)
{
throw new Exception("Invalid node found");
}
codesNode.RemoveChild(codeNode);
return schemaDocument.OuterXml;

Duplicate the single quote, so that it reads "Dupuytren''s contracture"
This way you can escape the single quote in the xpath expression.

Related

How to use SyntaxFactory for roslyn

I am trying to generate code using SyntaxFactory but stuck in converting one area Please see the code below I left comment where I am finding it difficult and used string manipulation.
The idea is there will be two variable removeExpression and getExpression which are of type ExpressionSyntax and finally both will be used to become the body of a anonymous function.
ExpressionSyntax removeExpression = SyntaxGenerator.MethodInvokeExpression(
valueExpression,
"RemoveAt",
BinaryExpression(
SyntaxKind.SubtractExpression,
SyntaxGenerator.PropertyAccessExpression(valueExpression, nameof(Enumerable.Count)),
atExpression
)
);
ExpressionSyntax getExpression = SyntaxGenerator.MethodInvokeExpression(valueExpression, nameof(Enumerable.First));
// Help me to convert the below line using SyntaxFactory.
IdentifierName("((Func<dynamic>)(() => { var res = " + getExpression.ToString() + ";" + removeExpression.ToString() + "; return res" + " ; }))(); ");

Clause Select in Datatable

I'm trying to do some Selects in a Datatable, but I am having some problems because I have values in some cells like this: 'COX-12-3SCS4CSCH
This value has ' and -
I tried to do this select but doesn't work:
string expression = "Nivel='" + lvfin + "' AND [Nivel " + lvfin + "]='" + codActual + "'";
DataRow[] results = DataTable.Select(expression);
lvfin contains for example 0 and codActual contains 'COX-12-3SCS4CSCH
And I get this error:
Missing operand after operator 'COX'
What is the problem here?
If your field name is Nivel 0 then you need to add that zero to the constant string "Nivel" and enclose the whole field name into square brackets to form the correct field name used in the first condition, then, if the value that you search contains a single quote, then you need to double it. All this is necessary to avoid confusing the parser when you use a single quote as delimiter for string values or your field names contains spaces.
So you should write (line by line for clarity but could be written in a single line):
string fieldName = "Nivel " + lvfin.ToString();
string searchValue = codActual.Replace("'", "''");
string expression = $"[{fieldName}]='{searchValue}'";
DataRow[] results = DataTable.Select(expression);

XMLPATH has an invalid token

I want to extract all the ndoes whose Date element has today's DateTime except seconds which is 10 seconds less than datetime.now.
Here is the code.
C#
DateTime total_seconds = Convert.ToDateTime(DateTime.Now.AddSeconds(-10));
string st = "_127.0.0.1";
//string v = "1";
XPathNodeIterator itr = nav.Select("pings/ping" + st + "[DATE<" + total_seconds + "]/DATE");
XML
<pings>
<ping_127.0.0.1>
<IP>127.0.0.1</IP>
<RTT>0</RTT>
<DATE>13-09-2015 16:47:09</DATE>
</ping_127.0.0.1>
<ping_127.0.0.1>
<IP>127.0.0.1</IP>
<RTT>1</RTT>
<DATE>13-09-2015 16:47:10</DATE>
</ping_127.0.0.1>
<ping_127.0.0.1>
<IP>127.0.0.1</IP>
<RTT>2</RTT>
<DATE>13-09-2015 16:47:11</DATE>
</ping_127.0.0.1>
<ping_127.0.0.1>
<IP>127.0.0.1</IP>
<RTT>1</RTT>
<DATE>13-09-2015 16:47:12</DATE>
</ping_127.0.0.1>
</pings>
This is the error
XPathNodeIterator itr = nav.Select("pings/ping" + st + "[DATE<" + total_seconds + "]/DATE");
While you haven't pasted in the error, from the title it says: "Invalid token". Your code suggests a string as XPath statement something along those lines:
pings/ping_127.0.0.1[DATE<37712828]/DATE
But since the total_seconds variable is of type DateTime, I assume it is a string something like "2015-10-09...13:22:56.584". Which is most likely the cause of your invalid token.
If not, can you put a breakpoint in your code and paste the resulting XPath?
Note: what you are trying to achieve is not (quite) possible, you cannot compare a date/time value with greater-than operators in XPath 1.0. It requires at least XPath 2.0 to do that. A workaround is, unfortunately, not trivial.

Crystal report textobject and fields

i am running the following code
foreach (ReportObject obj in oSectionObjects)
{
if (obj.Kind == CrystalDecisions.Shared.ReportObjectKind.TextObject)
{
// do stuff
}
}
but i have a problem. i do have multiple text that do contain text AND fields in them.
But crystal return me the field being TextObject which is technically true.
How do i know i ONLY have text in the TextObject and not anything else (aka fields, parameters, formulas) ?
As far as I know the fields in a text box will be recognized by the text pattern. Try to search the text of the text object for {1#xxxxx} where xxxxx is the field name. "{1#" shows the type of the field: 1 is for a database , 2 is for formula, 3 is for parameter. You may try also for {#xxxxx} *(without numeric field identifier)
I searched alot around and found working solution for RAS report but nothing for crystal. Anyhow if someone end up here looking for an answer here's the work around.
Whenever you have to concatenate multiple fields on the report do NOT use TextObject. Instead use a Formula. The formula fields wont bet part of the ReportObjects but instead part of the ReportDocument.DataDefinition.FormulaFields with Kind being CrystalDecisions.Shared.FieldKind.FormulaField and you will want to check the ValueType so it is CrystalDecisions.Shared.FieldValueType.StringField.
then you can manipulate them.
I did need that for translation of report live so here's a parsing method for formulas :
try
{
var sFormula = formula.Text;
string pattern = "\"[\\w ]*\"";
Regex r = new Regex(pattern);
MatchCollection mc = r.Matches(sFormula);
foreach (Match m in mc)
{
var sValue =m.Value;
var sParsedValue = sValue.Substring(1, sValue.Length - 2);
if (sParsedValue.StartsWith("s"))
{
var stest = "\"" + CApplicationData.TranslateStringValue(sParsedValue) + "\"";
sFormula = sFormula.Replace(sValue, stest);
}
}
formula.Text = sFormula;
}
catch{}
this above you will notice i use 's' as a key to know it might be a value to be translated so it's not mandatory. using the above on this formula with Spanish language :
"sPage" + " " + totext(PageNumber) + " " + "sOf" + " " + totext(TotalPageCount)
will modify the formula to :
"Página" + " " + totext(PageNumber) + " " + "de" + " " + totext(TotalPageCount)
giving output of :
Página 1 de 4

c# parsing xml with and apostrophe throws exception

I am parsing an xml file and am running into an issue when trying find a node that has an apostrophe in it. When item name does not have this everything works fine. I have tried replacing the apostrophe with different escape chars but am not having much luck
string s = "/itemDB/item[#name='" + itemName + "']";
// Things i have tried that did not work
// s.Replace("'", "''");
// .Replace("'", "\'");
XmlNode parent = root.SelectSingleNode(s);
I always receive an XPathException. What is the proper way to do this. Thanks
For apostophe replace it with &apos;
You can do it Like this:
XmlDocument root = new XmlDocument();
root.LoadXml(#"<itemDB><item name=""abc'def""/></itemDB>");
XmlNode node = root.SelectSingleNode(#"itemDB/item[#name=""abc'def""]");
Note the verbatim string literal '#' and the double quotes.
Your code would then look like this and there is no need to replace anything:
var itemName = #"abc'def";
string s = #"/itemDB/item[#name=""" + itemName + #"""]";

Categories

Resources