-1

I have a xml:

     <query xmlns='http://jabber.org/protocol/disco#info'>
        <identity category='store'
              type='file'
              name='HTTP File Upload' />
        <feature var='urn:xmpp:http:upload:0' />
        <x type='result' xmlns='jabber:x:data'>
            <field var='FORM_TYPE' type='hidden'>
                <value>urn:xmpp:http:upload:0</value>
            </field>
            <field var='max-file-size'>
                <value>5242880</value>
            </field>
        </x>
      </query>

I need to get data only from <value> tags. In a result I'd like to get List<string>. How to do this using System.Xml.Serialization?

Thanks for help

arex
  • 1
  • 1
  • What does “to get data only from tags” mean, and why must you use System.Xml.Serialization? – Dour High Arch Sep 24 '19 at 16:27
  • What would you like the result to look like? – Xtros Sep 24 '19 at 16:29
  • i am sorry I used words in <> brackets and they weren't displayed. :) And I would like to see how it is done with XmlSerialization but i don't have to use it – arex Sep 24 '19 at 17:32
  • You have deserialization as on of your tags but you have serialization in your question. I think you want serialization to serialize into C# class to get the XML attributes? – Deleted Sep 24 '19 at 17:38

2 Answers2

0

Please do your own thorough research before posting a question. Here are a few methods, your XML has namespaces(Xmlns) too so you need to consider that, go to the source credit for more examples.

- Method XML Namespace (you will most likely need this method) https://techoctave.com/c7/posts/113-c-reading-xml-with-namespace

- Method XMLnode
`
XmlDocument doc = new XmlDocument();    
doc.Load(Path);
XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
    string attrVal = elemList[i].Attributes["SuperString"].Value;
}

`

- Method Serialization

`

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var title = new Title() { Id = 3, Value = "something" };
            var serializer = new XmlSerializer(typeof(Title));
            var stream = new MemoryStream();
            serializer.Serialize(stream, title);
            stream.Flush();
            Console.Write(new string(Encoding.UTF8.GetChars(stream.GetBuffer())));
            Console.ReadLine();
        }
    }

    public class Title
    {
        [XmlAttribute("id")]
        public int Id { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

}
`

Source Credit: Serialize a C# class to XML with attributes and a single value for the class with-attributes-and-a-single-value-for-the-clas

Read XML Attribute using XmlDocument xmldocument

Deleted
  • 91
  • 1
  • 13
0

Thanks Deleted for your response but I relized that I need data from <field> and <value>

     <field var='FORM_TYPE' type='hidden'>
         <value>urn:xmpp:http:upload:0</value>

Fortunetly I figured it out by myself:

    [XmlType("field")]
    public class Field
    {

        [XmlAttribute("var")]
        public string Var { get; set; }


        [XmlElement("value")]
        public string Value { get; set; }

    }
    [XmlType("query")]
    public class DiscoFileInfo
    {
        [XmlArray("x",Namespace = "jabber:x:data")]
        [XmlArrayItem("field", typeof(Field))]
        public List<Field> Fields { get; set; }

    }
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(DiscoFileInfo), "http://jabber.org/protocol/disco#info");
arex
  • 1
  • 1