使用c#从XML获取属性

时间:2017-12-07 06:04:47

标签: c# .net xml linq-to-xml xmldocument

我在字符串变量中有以下xml内容。我想以字符串的形式获取每个属性(JID)的值。

下面是我的xml

  <query xmlns="http://jabber.org/protocol/muc#admin">
  <item affiliation="member" jid="a@something.com" />
  <item affiliation="member" jid="b@something.com" />
  <item affiliation="member" jid="c@something.com" />
  <item affiliation="member" jid="d@something.com" />
  <item affiliation="member" jid="e@something.com" />
  <item affiliation="member" jid="f@something.com" />
  <item affiliation="member" jid="g@something.com" />
  <item affiliation="member" jid="h@something.com" />
  <item affiliation="member" jid="i@something.com" />
  <item affiliation="member" jid="j@something.com" />
  </query>

任何人都可以提出一些想法来找到这个。 我的预期输出就像

   a@something.com
   b@something.com
   c@something.com
   d@something.com
   e@something.com
   f@something.com

4 个答案:

答案 0 :(得分:1)

Linq to XML是解决它的一种方法。在哪里&#34;&#34;在.Parse中 - 将字符串变量与xml放在一起。

           List<XAttribute> jids = XDocument.Parse("").Root.Elements().Attributes().ToList();
            foreach (XAttribute a in jids)
            {
              if (a.Name =="jid") 
              {
                string jid = a.Value;
              }
            }

答案 1 :(得分:0)

DataSet dsXml = new DataSet();
dsXml.ReadXmlSchema(Server.MapPath("~/Temp") + "//" + FileName);
dsXml.ReadXml(Server.MapPath("~/Temp") + "//" + FileName, XmlReadMode.InferTypedSchema);
 if (!string.IsNullOrEmpty(dsXml.GetXml()))
{
  for (int i = 0; i < dsXml.Tables.Count; i++)
            {

lblInfo.Text = lblInfo.Text + dsXml.Tables["item"].Rows[i]["jid"].ToString()
           }
}

答案 2 :(得分:0)

试试这个

  var data = @" <query xmlns=""http://jabber.org/protocol/muc#admin"">
  <item affiliation=""member"" jid=""a@something.com"" />
  <item affiliation=""member"" jid=""b@something.com"" />
  <item affiliation=""member"" jid=""c@something.com"" />
  <item affiliation=""member"" jid=""d@something.com"" />
  <item affiliation=""member"" jid=""e@something.com"" />
  <item affiliation=""member"" jid=""f@something.com"" />
  <item affiliation=""member"" jid=""g@something.com"" />
  <item affiliation=""member"" jid=""h@something.com"" />
  <item affiliation=""member"" jid=""i@something.com"" />
  <item affiliation=""member"" jid=""j@something.com"" />
  </query>";
            var xml = XElement.Parse(data);
            var r = xml.Elements();
            var result = r.Select(item => item.Attribute("jid")?.Value).ToArray();

答案 3 :(得分:0)

要实现所需的输出,您可以使用XMLDocument,XMLNode和XMLNodeList,如下所示

string sb = "<query xmlns=\"http://jabber.org/protocol/muc#admin\">" +
                         "  <item affiliation=\"member\" jid=\"a@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"b@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"c@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"d@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"e@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"f@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"g@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"h@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"i@something.com\" />" +
                         "  <item affiliation=\"member\" jid=\"j@something.com\" />" +
                         "  </query>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sb);

            XmlNodeList allXmlNode = doc.GetElementsByTagName("item");
            if (allXmlNode.Count >= 1)
            {
                foreach (XmlNode node in allXmlNode)
                {
                    System.Console.WriteLine(node.Attributes[1].InnerText);
                }
            }