【问题标题】:c# - how to deserialize only part of xml filec# - 如何仅反序列化部分xml文件
【发布时间】:2019-09-24 16:16:49
【问题描述】:

我有一个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>

我只需要从&lt;value&gt; 标签中获取数据。结果我想得到List&lt;string&gt;。 如何使用 System.Xml.Serialization 做到这一点?

感谢您的帮助

【问题讨论】:

  • “仅从标签中获取数据”是什么意思,为什么必须使用 System.Xml.Serialization?
  • 您希望结果是什么样的?
  • 很抱歉我在 括号中使用了单词,但没有显示出来。 :) 我想看看它是如何使用 XmlSerialization 完成的,但我不必使用它
  • 您的标签有反序列化,但您的问题有序列化。我认为您希望序列化序列化为 C# 类以获取 XML 属性?

标签: c# xml deserialization


【解决方案1】:

请在发布问题之前进行自己的彻底研究。这里有一些方法,你的 XML 也有命名空间(Xmlns),所以你需要考虑这一点,去源代码获取更多示例。

- 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; }
    }

}
`

来源信用: Serialize a C# class to XML with attributes and a single value for the class 具有属性和单个值的类

Read XML Attribute using XmlDocument xml文档

【讨论】:

    【解决方案2】:

    感谢删除您的回复,但我知道我需要来自 &lt;field&gt;&lt;value&gt; 的数据

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

    幸好是我自己想出来的:

        [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");
    

    【讨论】:

      猜你喜欢
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多