开发者

What's wrong with my xml?

i have the following xml:

<messageContent xmlns="http://tempuri.org/" >
<Message type="MappedMessage" >
<Properties RequestId="Point-CurveRequest-8326ad44-a1cd-4a96-b4ef-1c4ad213d940"  Action="getParCurves"  EESId="EESID:NY:20100128:BASE"  Currency="USD"  Index="INX" />
<Body></Body>
</Message>
</messageContent>

and then i have this query:

var messageType = xmlDoc.SelectSingleNode("/messageContent/Message[@type]");

but no matter what i've tried, i was never able to get the node that i'm looking for. Basically i'm just try to see if there is a node (named "Message") which has a "type开发者_如何学Python" property inside of it.

Has anyone have any idea here?


There's absolutely nothing wrong with your XML - there's something wrong with your XPath expression, though :-)

Add a XML namespace manager to your code:

XmlNamespaceManager mgr = new XmlNamespaceManager(xdoc.NameTable);
mgr.AddNamespace("ns", "http://tempuri.org/");

and then use that namespace manager when you do your SelectSingleNode:

var messageType = xdoc.SelectSingleNode("/ns:messageContent/ns:Message[@type]", mgr);

That should work.


You seem to need to establish a namespace context on your xpath, or get rid of that xmlns="http://tempuri.org/".


NOt a straight forward but will work

XMLElement messageElement = (XMLElement) xmlDoc.SelectSingleNode("/messageContent/Message");
if(messageElement.HasAttribute("type"))

But the thing is if you have a node Message butdoes not contain type attribute then its not proper format of an xml.Rather i would suggest to check the content of the type attribute like below

XMLNode messageElement = xmlDoc.SelectSingleNode("/messageContent/Message[@type='MappedMessage']");
if(messageElement != null)
{
//Do SOmething
}


If messageContent is the root node you should omit it like: /Message[@type] or if you want to check the entire hierarchy of the document: //Message[@type]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜