all groups > dotnet xml > january 2006 >
You're in the

dotnet xml

group:

Error message when retriving a string from my xml file in C#.net a


Error message when retriving a string from my xml file in C#.net a Alpha
1/12/2006 2:57:02 PM
dotnet xml:
I created the following code in my C# program but it's giving me error
message at run time of :

XML.XPATH.XPATHEXCEPTION : Namespace Manager or XSLTContext needed. This
query has a prefix, variable or user defined function.


Can someone see what I'm doing wrong? Thanks, Alpha

private XPathDocument unityMessages = new XPathDocument("UnityMessages.xml");

unityMsgNavigator = unityMessages.CreateNavigator();

string query = @"/trans-unit[@id=""m1""]/target[@xml:lang=""fr""]";

XPathExpression queryM1 = unityMsgNavigator.Compile(query);

m1 = (string)unityMsgNavigator.Evaluate(queryM1);



Here is part of the content from my xml file:
<?xml version="1.0" encoding="utf-8" ?>
<xliff version="1.1">
<trans-unit id="m1">
<source xml:lang="en-us">Error populating the children nodes of the
selected node.</source>
<target xml:lang="fr">Translation of "Vous le vou coucher avec moi
sesua?"</target>
</trans-unit>
</xliff>


Re: Error message when retriving a string from my xml file in C#.n Alpha
1/13/2006 9:54:02 AM
This is what I have in the code but it's still not diaplaying any messages.
However, I did get rid of that Name Sapce Manager Error message though.
Thank you. Can you please have a look again at my code and tell me what I'm
doing wrong here. Thanks a million. I'm trying to set up a XML file for
capturing all messages string for futuer localization usages.
Thanks, Alpha


try
{
unityMessages = new
XPathDocument(@"C:\PROJECTS\PPGLOBAL\UnityMessages.xml");
unityMsgNavigator = unityMessages.CreateNavigator();

string query =
@"/trans-unit[@id=""m1""]/target[lang(""fr"")]";
XPathExpression queryM1 = unityMsgNavigator.Compile(query);
XPathNodeIterator ni =
(XPathNodeIterator)unityMsgNavigator.Evaluate(queryM1);
while (ni.MoveNext())
{
MessageBox.Show(ni.Current.ToString());
}


This is what I have in the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<xliff version="1.1">
<trans-unit id="m1">
<source xml:lang="en-us">Error populating the children nodes of the
selected node.</source>
<target xml:lang="fr">Translation of "Vous le vou coucher avec moi
sesua?"</target>
</trans-unit>
</xliff>


[quoted text, click to view]
Re: Error message when retriving a string from my xml file in C#.n Alpha
1/13/2006 11:05:02 AM
Yes, thank you so very much. It is working now. I just have one more
question. Why doesn't this line of code worked?
string m1 = (string)unityMsgNavigator.Evaluate(queryM1);
I thought the Evalute method returns the typed result but I'm getting
messages about it can't cast the type XPathNodeIterator to string.

Thanks, Alpha

[quoted text, click to view]
Re: Error message when retriving a string from my xml file in C#.net a Martin Honnen
1/13/2006 1:47:49 PM


[quoted text, click to view]


[quoted text, click to view]

You are using the prefix xml in the attribute name xml:lang without
using a namespace manager.
Either use a namespace manager or simply do
string query = @"/trans-unit[@id=""m1""]/target[lang(""fr"")]"

--

Martin Honnen --- MVP XML
Re: Error message when retriving a string from my xml file in C#.n Martin Honnen
1/13/2006 7:08:54 PM


[quoted text, click to view]


[quoted text, click to view]


[quoted text, click to view]

You want this XPath expression (well C# string literal with an XPath
expression) then
@"/xliff/trans-unit[@id=""m1""]/target[lang(""fr"")]"

--

Martin Honnen --- MVP XML
Re: Error message when retriving a string from my xml file in C#.n Aaron
1/14/2006 8:21:13 AM
Martin

"XPath expressions can be of four types, a string or a boolean or a=20
number or a node set."
Is that only for Xpath 1.0.
2.0 introduces support for the XML Schema primitive types, which =
immediately gives the user access to 19 simple types, including dates, =
years, months, URIs and others?

Aaron=20


[quoted text, click to view]
Re: Error message when retriving a string from my xml file in C#.n Martin Honnen
1/14/2006 2:13:44 PM


[quoted text, click to view]

XPath expressions can be of four types, a string or a boolean or a
number or a node set. Most XPath expressions used evaluate to a node set
and will be treated as an XPathNodeIterator then in .NET code outside of
XPath.
If you have an XPath expression explictly returning e.g. a number for
instance
double count = (double)navigator.Evaluate("count(//*)")
then such a cast works.
To explicity return a string you could do e.g.
string name = (string)navigator.Evaluate("local-name(/*)")
or
string someValue = (string)navigator.Evaluate("string(/root/element)")

--

Martin Honnen --- MVP XML
Re: Error message when retriving a string from my xml file in C#.n Martin Honnen
1/14/2006 5:24:56 PM


[quoted text, click to view]


[quoted text, click to view]

Yes, and my answer was in a post about a specific problem with using
XPath from .NET code where .NET only supports XPath 1.0 so I don't see
why it should have been more generic.

[quoted text, click to view]

XPath 2.0 and its data model indeed tries to (re)use the W3C schema data
types but that does not help when using the XPath API in current .NET
versions.

--

Martin Honnen --- MVP XML
Re: Error message when retriving a string from my xml file in C#.n Alpha
1/16/2006 4:48:02 PM
Hi Martin,

I tried the following code but I got an error message about illegal token in
the string. I also tried the 2nd string type you suggested and got the same
error. Can you have a look and let me know how I should correct this?

Thank you, Alpha

string query =
@"xliff/trans-unit[@id=""m1""]/target[lang(""fr"")](/root/element)";

string query = @"xliff/trans-unit[@id=""m1""]/target[lang(""fr"")](/*)";

XPathExpression queryM1 = unityMsgNavigator.Compile(query);
m1 = (string)unityMsgNavigator.Evaluate(queryM1);


[quoted text, click to view]
AddThis Social Bookmark Button