C#:将对象渲染为XML

时间:2009-02-23 18:27:03

标签: c# xml serialization

我正在寻找一种将对象树转换为XML的方法。写作会很有趣,但我相信有人已经写过了。这是我的愿望清单:

  • 它不应该关心构造函数
  • 理想情况下应该处理循环引用(不要过于讨论如何)
  • 不应要求更改对象 - 例如,没有自定义属性
  • 它不应该关心或需要已知类型(例如,XmlInclude)
  • XML应该简单易懂 - 操作团队成员需要人类可读
  • 如果某个属性无法序列化,则应该只是禁止错误并继续
  • 可以处理列表和词典

我不需要重建对象模型,因此只写解决方案很好(可能是预期的)。

我认为折扣:

  • XmlSerializer - 需要无参数构造函数,没有循环引用支持
  • DataContractSerializer - 需要属性(选择加入)

3 个答案:

答案 0 :(得分:6)

罗伯特罗斯尼的帖子让我觉得这可能比我想象的要少。所以这是一次非常艰难的尝试。它处理以下内容:

  • 如果无法读取属性,则会将异常打印为值
  • 循环引用和多次出现。它将ID与每个元素相关联;如果一个元素出现两次,它只是指向ref ID。 Ref ID对象图是唯一的(我应该使用GUID,但这符合我的目的)。
  • 派生类型没有问题
  • 它不需要任何属性或特定构造函数或其他无意义的
  • 它可以处理只读属性

以下是输出示例(在我的测试对象中,Order上的“Currency”产品会抛出异常)。

<Customer Ref="1">
  <FirstName>Paul</FirstName>
  <LastName>Stovell</LastName>
  <FullName>Paul Stovell</FullName>
  <Orders>
    <Order Ref="2">
      <SKU>Apples</SKU>
      <Price>27.30</Price>
      <Currency>Something bad happened</Currency>
      <Customer Ref="1" />
    </Order>
    <Order Ref="3">
      <SKU>Pears</SKU>
      <Price>17.85</Price>
      <Currency>Something bad happened</Currency>
      <Customer Ref="1" />
    </Order>
    <Order Ref="2" />
  </Orders>
</Customer>

以下是示例对象模型和用法:

static void Main(string[] args)
{
    var customer = new Customer();
    customer.FirstName = "Paul";
    customer.LastName = "Stovell";
    customer.Orders.Add(new Order(customer) { Price = 27.30M, SKU = "Apples"});
    customer.Orders.Add(new Order(customer) { Price = 17.85M, SKU = "Pears"});
    customer.Orders.Add(customer.Orders[0]);

    var output = new StringWriter();
    var writer = new XmlTextWriter(output);
    writer.Formatting = Formatting.Indented;
    WriteComplexObject("Customer", customer, writer);
    Console.WriteLine(output.ToString());
    Console.ReadKey();
}

class Customer
{
    private readonly List<Order> _orders = new List<Order>();

    public Customer()
    {
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string FullName
    {
        // Read-only property test
        get { return FirstName + " " + LastName; }
    }

    public List<Order> Orders
    {
        // Collections test
        get { return _orders; }
    }
}

class Order
{
    private readonly Customer _customer;

    public Order(Customer customer)
    {
        _customer = customer;
    }

    public string SKU { get; set; }
    public decimal Price { get; set; }
    public string Currency
    {
        // A proprty that, for some reason, can't be read
        get
        {
            throw new Exception("Something bad happened");
        }
    }

    public Customer Customer
    {
        get { return _customer; }
    }
}

以下是实施:

public static void WriteObject(string name, object target, XmlWriter writer)
{
    WriteObject(name, target, writer, new List<object>(), 0, 10, -1);
}

private static void WriteObject(string name, object target, XmlWriter writer, List<object> recurringObjects, int depth, int maxDepth, int maxListLength)
{
    var formatted = TryToFormatPropertyValueAsString(target);
    if (formatted != null)
    {
        WriteSimpleProperty(name, formatted, writer);
    }
    else if (target is IEnumerable)
    {
        WriteCollectionProperty(name, (IEnumerable)target, writer, depth, maxDepth, recurringObjects, maxListLength);
    }
    else
    {
        WriteComplexObject(name, target, writer, recurringObjects, depth, maxDepth, maxListLength);
    }
}

private static void WriteComplexObject(string name, object target, XmlWriter writer, List<object> recurringObjects, int depth, int maxDepth, int maxListLength)
{
    if (target == null || depth >= maxDepth) return;
    if (recurringObjects.Contains(target))
    {
        writer.WriteStartElement(name);
        writer.WriteAttributeString("Ref", (recurringObjects.IndexOf(target) + 1).ToString());
        writer.WriteEndElement();
        return;
    }
    recurringObjects.Add(target);

    writer.WriteStartElement(name);
    writer.WriteAttributeString("Ref", (recurringObjects.IndexOf(target) + 1).ToString());
    foreach (var property in target.GetType().GetProperties())
    {
        var propertyValue = ReadPropertyValue(target, property);
        WriteObject(property.Name, propertyValue, writer, recurringObjects, depth + 1, maxDepth, maxListLength);
    }
    writer.WriteEndElement();
}

private static object ReadPropertyValue(object target, PropertyInfo property)
{
    try { return property.GetValue(target, null); }
    catch (Exception ex) { return ReadExceptionMessage(ex); }
}

private static string ReadExceptionMessage(Exception ex)
{
    if (ex is TargetInvocationException && ex.InnerException != null)
        return ReadExceptionMessage(ex.InnerException);
    return ex.Message;
}

private static string TryToFormatPropertyValueAsString(object propertyValue)
{
    var formattedPropertyValue = null as string;
    if (propertyValue == null)
    {
        formattedPropertyValue = string.Empty;
    }
    else if (propertyValue is string || propertyValue is IFormattable || propertyValue.GetType().IsPrimitive)
    {
        formattedPropertyValue = propertyValue.ToString();
    }
    return formattedPropertyValue;
}

private static void WriteSimpleProperty(string name, string formattedPropertyValue, XmlWriter writer)
{
    writer.WriteStartElement(name);
    writer.WriteValue(formattedPropertyValue);
    writer.WriteEndElement();
}

private static void WriteCollectionProperty(string name, IEnumerable collection, XmlWriter writer, int depth, int maxDepth, List<object> recurringObjects, int maxListLength)
{
    writer.WriteStartElement(name);
    var enumerator = null as IEnumerator;
    try
    {
        enumerator = collection.GetEnumerator();
        for (var i = 0; enumerator.MoveNext() && (i < maxListLength || maxListLength == -1); i++)
        {
            if (enumerator.Current == null) continue;
            WriteComplexObject(enumerator.Current.GetType().Name, enumerator.Current, writer, recurringObjects, depth + 1, maxDepth, maxListLength);
        }
    }
    catch (Exception ex)
    {
        writer.WriteElementString(ex.GetType().Name, ReadExceptionMessage(ex));
    }
    finally
    {
        var disposable = enumerator as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
        writer.WriteEndElement();
    }
}

我仍然有兴趣知道是否有更多经过验证的解决方案。

答案 1 :(得分:4)

这似乎可以直接使用反射来编写:给定一个对象实例,创建一个带有类名的XML元素,然后迭代它的所有属性。

为每个属性创建一个名称为

的元素
  • 如果它是值类型,则将其文本设置为其值的XML Schema文本;
  • 如果它实现IEnumerable,则迭代它并为每个项创建一个元素;
  • 如果是任何其他引用类型,请将元素的内容设置为属性的XML表示。

使用包含您已序列化的每个对象的哈希码的HashSet跟踪循环/多个引用;如果你在HashSet中找到一个对象的哈希码,你已经将它序列化了。 (如果发生这种情况,我不知道你想把它放到XML中。)

但不,我没有任何代码可以解决这个问题。

答案 2 :(得分:1)

我怀疑你会在所有课程中发现任何特别有用的东西。正如您所指出的,到目前为止,XmlSerializer是微软在通用终端上做出的最大努力。

另一端是visualizers,它们对于特定的类是唯一的。我认为还没有多少幸福的媒介。