本文介绍了将 XML 部分反序列化为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些 XML 反序列化为业务对象.我正在使用 XmlSerializer.Deserialize 这样做.但是,我希望 XML 中包含的 XmlElement 之一保持 XElement.

I have some XML that I deserialize into a business object. I am using XmlSerializer.Deserialize to do so. However, I want one of the XmlElement contained in the XML to stay an XElement.

它不能直接完成(使用 XmlElementAttribute),因为 XElement 不可序列化.我还尝试将该元素序列化为字符串(分两步尝试获取 XElement),但失败并显示错误:

It cannot be done directly (using an XmlElementAttribute) since XElement is not Serializable. I also tried to serialize that element to a string (in a two steps attempt to get an XElement), but that failed with the error:

意外的节点类型元素.readelementstring 方法只能是用简单的或空内容

知道如何做到这一点吗?

Any idea how that can be done?

这是我想要的 xml 和结果对象的示例:

Here is an example of xml and the resulting object I want:

<Person name="Joe">
  <Hobbies>
    <Hobby name="Reading" .../>
    <Hobby name="Photography" .../>
  </Hobbies>
  <HomeAddress>
    ...
  </HomeAddress>
</Person>

对象:

 public class Person
    {
      [XmlAttribute("Name")]
      public string Name {get; set;}
      ?????
      public XElement Hobbies {get; set;}
      [XmlElement("HomeAddress")]
      public Address HomeAddress {get; set;}
    }

无效的尝试:

[XmlElement("Hobbies")]
public XElement Hobbies {get; set;}
[XmlElement("Hobbies")]
public string Hobbies {get; set;}

推荐答案

为了避免实现诸如 IXmlSerializable 之类的东西的繁重工作,您可以按照半隐藏传递的方式做一些事情XmlElement 属性;但是请注意,这并不能完全满足您的要求,因为您只能拥有一个根 XElement 值(不是两个,根据您的示例);你需要一个清单来做到这一点......

To avoid the hard work of implementing something like IXmlSerializable, you might do something along the lines of a semi-hidden pass-thru XmlElement property; note, however, that this doesn't quite do what you want since you can only have one root XElement value (not two, as per your example); you would need a list to do that...

using System;
using System.ComponentModel;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
public class Person
{
    [XmlAttribute("Name")]
    public string Name { get; set; }
    [XmlIgnore]
    public XElement Hobbies { get; set; }

    [XmlElement("Hobbies")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public XmlElement HobbiesSerialized
    {
        get
        {
            XElement hobbies = Hobbies;
            if(hobbies == null) return null;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(hobbies.ToString());
            return doc.DocumentElement;
        }
        set
        {
            Hobbies = value == null ? null
                : XElement.Parse(value.OuterXml);
        }
    }
    [XmlElement("HomeAddress")]
    public Address HomeAddress { get; set; }
}

public class Address { }

static class Progmam
{
    static void Main()
    {
        var p = new Person { Hobbies = new XElement("xml", new XAttribute("hi","there")) };
        var ser = new XmlSerializer(p.GetType());
        ser.Serialize(Console.Out, p);
    }
}

这篇关于将 XML 部分反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:19