解析 XMPP 内部 xml

Parse XMPP inner xml

<?xml version="1.0" encoding="UTF-8"?>
<message xmlns="jabber:client" to="dev_345@localhost/unityXMPP" type="chat" xml:lang="en" from="dev_272@localhost/unityXMPP">
   <archived xmlns="urn:xmpp:mam:tmp" id="1503375414608430" by="dev_345@localhost" />
   <stanza-id xmlns="urn:xmpp:sid:0" id="1503375414608430" by="dev_345@localhost" />
   <body>hi</body>
</message>

我想解析内部 XML 以获取 id 属性。 我已经创建了我发现的任何名称空间。我能够从属性中得到。下面是c#中的代码。

string value = "<message xmlns=\"jabber:client\" to=\"dev_345@localhost/unityXMPP\" type=\"chat\" xml:lang=\"en\" from=\"dev_272@localhost/unityXMPP\"><archived xmlns=\"urn:xmpp:mam:tmp\" id=\"1503375414608430\" by=\"dev_345@localhost\" /><stanza-id xmlns=\"urn:xmpp:sid:0\" id=\"1503375414608430\" by=\"dev_345@localhost\" /><body>hi</body></message>";

    XmlDocument xmlDoc = new XmlDocument ();
    XmlNamespaceManager namespaces = new XmlNamespaceManager (xmlDoc.NameTable);
    namespaces.AddNamespace ("ns", "jabber:client");
    namespaces.AddNamespace ("ns1", "urn:xmpp:mam:tmp");
    xmlDoc.LoadXml (value);
    XmlNode messageNode = xmlDoc.SelectSingleNode ("/ns:message", namespaces);
    string sender = messageNode.Attributes ["from"].Value;
    string receiver = messageNode.Attributes ["to"].Value;
    string message = messageNode.InnerText;
    XmlNode timeStampNode = xmlDoc.SelectSingleNode ("/ns:message/ns1:archived");
    string timestamp = timeStampNode.Attributes ["id"].Value;

最好使用 XPath,如果您不想 de-/serialize 将 xml 转换为对象 (Link)。

或者您可以使用序列化,这是在您的解决方案 (Link 1, Link 2) 中使用 json 或 xml 的一种非常简单的方法。

这有帮助吗?我使用 LINQ To Xml

string xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message xmlns=\"jabber:client\" to=\"dev_345@localhost/unityXMPP\" type=\"chat\" xml:lang=\"en\" from=\"dev_272@localhost/unityXMPP\">   <archived xmlns=\"urn:xmpp:mam:tmp\" id=\"1503375414608430\" by=\"dev_345@localhost\" />   <stanza-id xmlns=\"urn:xmpp:sid:0\" id=\"1503375414608430\" by=\"dev_345@localhost\" />   <body>hi</body></message>";
var xdoc = XDocument.Parse(xmltext);
foreach (var item in xdoc.Root.Descendants())
{
    if (item.Name.LocalName == "archived")
    Console.WriteLine(item.Attribute("id").Value);                
}

尝试按照 xml linq 获取所有数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var message = doc.Descendants().Where(x => x.Name.LocalName == "message").Select(x => new {
                to = (string)x.Attribute("to"),
                type = (string)x.Attribute("type"),
                lang = (string)x.Attributes().Where(y => y.Name.LocalName == "lang").FirstOrDefault(),
                from = (string)x.Attribute("from"),
                messages = x.Elements().Select(y => new {
                    name = y.Name.LocalName,
                    id = (string)y.Attribute("id"),
                    by = (string)y.Attribute("by"),
                    value = (string)y
                }).ToList()
            }).FirstOrDefault();
        }
    }
}