Help parsing XML, simple string - but I can't parse it

I have the following XML:

<iq xmlns="jabber:client" to="39850777771287777738178727@guest.google.com/agsXMPP" xml:lang="en" id="sub23" from="search.google.com" type="result">
    <pubsub xmlns="http://jabber.org/protocol/pubsub">
        <subscription subscription="subscribed" subid="5077774B57777BD77770" node="search" jid="39850777771287777738178727@guest.google.com/agsXMPP" />
    </pubsub>
</iq>

I tried parsing from linq to sql, but it doesn't seem to understand that these are different nodes. It groups all iq into one element.

Can someone help with parsing this using XML?

The data I want to get is subid = "5077774B57777BD77770" and id = "sub23"

Thank!

Edit:

Here the code that I have tried to do this in two ways:

XDocument doc = XDocument.Parse("<xml>" + iq.ToString() + "</xml>");
        var results = from feed in doc.Elements("xml")
                       select new
                       {
                           Id = (string)feed.Element("iq").Attribute("id"),
                           Subid = (string)feed.Element("iq").Element("pubsub").Element("subscription").Attribute("subid")
                       };

and

                var doc = new System.Xml.XmlDocument();
            doc.LoadXml(iq.ToString());
            var searchId = doc.Attributes["id"];
            var subid = doc.SelectSingleNode("/pubsub/subscription").Attributes["subid"];
+3
source share
4 answers

As Dimitar noted, you have a problem with the namespace. This will work:

    using System;
using System.Xml;

namespace XMLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
             XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
            namespaces.AddNamespace("ns1", "jabber:client");
            namespaces.AddNamespace("ns2", "http://jabber.org/protocol/pubsub");
            doc.Load("xmltest.xml");

            XmlNode iqNode = doc.SelectSingleNode("/ns1:iq", namespaces);
            string ID = iqNode.Attributes["id"].Value;
            Console.WriteLine(ID);

            XmlNode subscriptionNode = doc.SelectSingleNode("/ns1:iq/ns2:pubsub/ns2:subscription", namespaces);
            string subID = subscriptionNode.Attributes["subid"].Value;
            Console.WriteLine(subID);

            Console.ReadLine();
        }
    }
}
+3
source

this , XPath, , XML.

+2

I'm not sure if this is what you need, but it works:

XNamespace jabber = "jabber:client";
XNamespace pubsub = "http://jabber.org/protocol/pubsub";

string xmltext = "<iq xmlns=\"jabber:client\" to=\"39850777771287777738178727@guest.google.com/agsXMPP\" xml:lang=\"en\" id=\"sub23\" from=\"search.google.com\" type=\"result\">\n"
    + "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">\n"
    + "<subscription subscription=\"subscribed\" subid=\"5077774B57777BD77770\" node=\"search\" jid=\"39850777771287777738178727@guest.google.com/agsXMPP\" />\n"
    + "</pubsub>\n"
    + "</iq>";

XDocument xdoc = XDocument.Parse(xmltext);

var iqelem = xdoc.Element(jabber + "iq");
var id = iqelem.Attribute("id").Value;

var subselem = iqelem.Element(pubsub + "pubsub").Element(pubsub + "subscription");
var subid = subselem.Attribute("subid").Value;

Console.WriteLine("SubId = {0}\nId={1}", subid, id);
+1
source

I agree with Dimitre - the "empty" xmlns namespace at the top is probably causing the problem. I sometimes shoot them with regex if they are not used, otherwise play with XmlNameSpaceManager as described

0
source

Source: https://habr.com/ru/post/1770030/


All Articles