首页 文章

XML反序列化

提问于
浏览
5

我有以下xml文件 .

<a>
  <b>
    <c>val1</c>
    <d>val2</d>
  </b>
  <b>
    <c>val3</c>
    <d>val4</d>
  </b>
<a>

我想将它反序列化为一个类,我想用创建的类的对象访问它们 . 我正在使用C# . 我能够反序列化并将值放入类' a '( <a> 标记)的对象中 . 但如何从这个对象访问 <b> 的值?我做了以下编码:

[Serializable()]
[XmlRoot("a")]
public class a
{
    [XmlArray("a")]
    [XmlArrayItem("b", typeof(b))]
    public b[] bb{ get; set; }
}

[Serializable()]
public class b
{
    [XmlElement("c")]
    public string c{ get; set; }
    [XmlElement("d")]
    public string d{ get; set; }    
}
class Program
{
        static void Main(string[] args)
        {

            a i = null;
            string path = "test.xml";

            XmlSerializer serializer = new XmlSerializer(typeof(a));

            StreamReader reader = new StreamReader(path);
            i = (a)serializer.Deserialize(reader);
            reader.Close();
            //i want to print all b tags here
            Console.Read();
        }
    }

5 回答

  • 2

    为此,您可以进行以下更改

    public class a
    {
        [XmlElement("b")]
        public b[] bb{ get; set; }
    }
    

    通过在数组上使用 XmlElement 属性,您实际上告诉序列化程序,数组元素应该序列化/反序列化为当前元素的直接子元素 .

    这是一个工作示例,我将XML放在一个字符串中只是为了使示例自包含 .

    using System;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          string xml =
            @"<a> 
                <b> 
                  <c>val1</c> 
                  <d>val2</d> 
                </b> 
                <b> 
                  <c>val3</c> 
                  <d>val4</d> 
                </b> 
              </a>";
    
          XmlSerializer xs = new XmlSerializer(typeof(a));
          a i = (a)xs.Deserialize(new StringReader(xml));
    
          if (i != null && i.bb != null && i.bb.Length > 0)
          {
            Console.WriteLine(i.bb[0].c); 
          }
          else
          {
            Console.WriteLine("Something went wrong!"); 
          }
    
          Console.ReadKey();
        }
      }
    
    
      [XmlRoot("a")]
      public class a
      {    
        [XmlElement("b")]
        public b[] bb { get; set; }
      }
    
      public class b
      {
        [XmlElement("c")]
        public string c { get; set; }
        [XmlElement("d")]
        public string d { get; set; }
      }  
    }
    
  • -1

    如果对创建xml序列化类有疑问,我发现解决问题的最简单方法是:

    • 将所有虚拟数据转储到XML文件中

    • 运行xsd.exe以创建.xsd模式文件

    • 在架构文件上运行xsd.exe以创建类文件

    我刚才在一篇博文中写了一篇关于它的快速教程:http://www.diaryofaninja.com/blog/2010/05/07/make-your-xml-stronglytyped-because-you-can-and-its-easy

    它需要不到一分钟,然后您可以轻松地从那里调整东西 . XSD.exe是你的朋友

  • 7

    我发现解决问题的最简单方法是将类 ab 的定义更改为以下

    public class b {
        public string c { get; set; }
        public string d { get; set; }
    }
    
    [XmlRoot(Namespace="", ElementName="a")]
    public class a : List<b> { }
    

    然后你的程序将工作 . 您可以选择添加 class b 属性 [XmlRoot (Namespace = "", ElementName = "b")]

  • 0
    class Program
    {
        static void Main(string[] args)
        {
            string employeedata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><name>test</bar></nmae>";//demo xml data
            using (TextReader sr = new StringReader(employeedata))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Employee));//pass type name in XmlSerializer constructor here
                Employee response = (Employee)serializer.Deserialize(sr);
                Console.WriteLine(response.name);
            }
    
        }
    }
    [System.Xml.Serialization.XmlRoot("tag")]
    public class Employee
    {
        public string name { get; set; }
    }
    
  • 0

    如果阻止(来自Chris Tyler的答案)改变如下 .

    if (i != null && i.bb != null && i.bb.Length > 0)
                {
                    foreach (b t in i.bb)
                    {
                        Console.WriteLine(t.c);
                        Console.WriteLine(t.d);
                    }
                }
    

    然后它会给你以下结果:

    val1 val2 val3 val4

相关问题