如何标记您的C#对象以反序列化此XML [英] How to mark up your C# object to Deserialize this XML

查看:116
本文介绍了如何标记您的C#对象以反序列化此XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信这是如此复杂...

I can't believe how unbelievably complicated this has been...

我有以下XML ...

I have the following XML...

<Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://mynamespace.com/books">
  <Rows>
    <Row>
      <Author><Name>Stephen King</Name></Author>
    </Row>  
   </Rows>
</Library>

我希望将反序列化的C#对象读为... Library.Books[0].Author

I would like the Deserialized C# object to read like... Library.Books[0].Author

我已经尝试使用百万种不同的XML属性标记组合来反序列化,例如...

I have tried a million different combination of XML Attribute Markings to Deserialize, such as...

[XmlRootAttribute("Library", Namespace = "http://mynamespace.com/books", IsNullable = false)]
public class Library
{
    [XmlElement("Rows")]
    public List<Book> Books { get; set; }
}

[XmlRoot("Row")]
public class Book
{
    public Author Author { get; set; }
}

[XmlRoot("Author")]
public class Author
{
    public string Name { get; set; }
}

...,当我尝试反序列化时,我不断获得"Author"对象为空.它几乎成功了...我在Books属性中得到了带有一个Book项目的ArrayList.但是对于我一生,我找不到作者.

...and I continually get the "Author" object as null when I try to Deserialze. It almost succeeds...I do get the ArrayList with one Book item in the Books property. But for the life of me I can't get the Author.

任何建议/帮助将不胜感激!

Any advice/help would be greatly appreciated!

推荐答案

尝试

public class Library
{
    [XmlArray("Rows")]
    [XmlArrayItem("Row")]
    public List<Book> Books { get; set; }
}

这篇关于如何标记您的C#对象以反序列化此XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆