-2

I have the following xml contents inside a string variable. i want to get the value of each attribute (JID) in the form of string.

Below is my 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>

Anyone please suggest some ideas to find this. my expected output is like

   a@something.com
   b@something.com
   c@something.com
   d@something.com
   e@something.com
   f@something.com
Lazy Programer
  • 171
  • 1
  • 1
  • 12

4 Answers4

1

Linq to XML is one way to tackle it. Where the "" inside .Parse is - place the string variable with the xml.

           List<XAttribute> jids = XDocument.Parse("").Root.Elements().Attributes().ToList();
            foreach (XAttribute a in jids)
            {
              if (a.Name =="jid") 
              {
                string jid = a.Value;
              }
            }
Cody Popham
  • 992
  • 5
  • 14
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()
           }
}
krunal shah
  • 51
  • 1
  • 9
  • I am not reading the xml from .xml File, i just read the xml content which is inside a string variable like `string xmlQuery=" ";` – Lazy Programer Dec 07 '17 at 06:20
0

try this

  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();
ali zarei
  • 1,212
  • 11
  • 17
0

To achieve the required output you can use XMLDocument , XMLNode and XMLNodeList as follows

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);
                }
            }
Swapnil
  • 66
  • 7