使用agsxmpp库从xml节点获取信息

时间:2016-11-25 09:57:06

标签: c# xml agsxmpp stanza

<query xmlns="urn:xmpp:dialogueHistory">
<dialogueHistory id="4d38f289-9">
<MessageID>26164</MessageID>
<messageText>Fhh</messageText>
<msgDate>11/25/2016 6:30:39 AM</msgDate>
<unReadCount>0</unReadCount>
</dialogueHistory>
</query>

获取messageid,messagetext

的信息

如何使用agsxmpp库

从上面的xml获取信息

名称空间为agsXMPP.Xml.Dom

1 个答案:

答案 0 :(得分:0)

您需要指定命名空间。使用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);

            List<XElement> dialogueHistory = doc.Descendants().Where(x => x.Name.LocalName == "dialogueHistory").ToList();
            XNamespace ns = dialogueHistory.FirstOrDefault().GetDefaultNamespace();

            var results = dialogueHistory.Select(x => new {
                id = (string)x.Attribute("id"),
                messageId = (int)x.Element(ns + "MessageID"),
                messageText = (string)x.Element(ns + "messageText"),
                messageDate = (DateTime)x.Element(ns + "msgDate"),
                unReadCount = (int)x.Element(ns + "unReadCount")
            }).ToList();

        }
    }
}