0

I have an XmlElement containing this data:

<message from="smithfamily@conference.hp63008-y57/admin" to="admin@hp63008-y57/Jabber.Net" type="groupchat" id="e83Dn-53" xmlns="jabber:client">
    <body>:d</body> 
    <x xmlns="jabber:x:event">
        <offline /> 
        <delivered /> 
        <displayed /> 
        <composing /> 
    </x>
    <delay stamp="2013-08-07T16:53:32.693Z" xmlns="urn:xmpp:delay" from="admin@hp63008-y57/Spark 2.6.3" /> 
    <x stamp="20130807T16:53:32" xmlns="jabber:x:delay" from="admin@hp63008-y57/Spark 2.6.3" /> 
</message>

I would like to get the attributes values stamp and from inside the delay element. I have tried several XPaths but I don't know exactly how to use it or if I have to declare a namespace.

Richard Cook
  • 32,523
  • 5
  • 46
  • 71
Darf Zon
  • 6,268
  • 20
  • 90
  • 149

1 Answers1

0

Use XElement instead. This will save you a lot of time and effort.

XElement xmlRoot = XElement.Load("someFile.xml");
XElement xmlRoot = XElement.Parse("someXmlString");

string stampValue = xmlRoot
    .Element("delay")
    .Attribute("stamp")
    .Value;

string fromValue = xmlRoot
    .Element("delay")
    .Attribute("from")
    .Value;

If you have more than one element use Elements, but that should be the basics of what you need.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • I can't still access, it returns null. I supposed I have to set a namespace or something like that – Darf Zon Aug 07 '13 at 20:01
  • @DarfZon, there should be no reason to set a namespace (I don't even know how you would anyways for reading). Break each of those statements up and put breakpoints. Go line by line and see where the problem is. – gunr2171 Aug 07 '13 at 20:06
  • @DarfZon, my first guess is that what you have posted is not the _complete_ xml, and that it is wrapped in something else. – gunr2171 Aug 07 '13 at 20:07