简体   繁体   English

XmlSerializer将C#对象转换为xml字符串问题

[英]XmlSerializer Convert C# object to xml string issue

I have the following code but i not want to show the node called "booksList". 我有以下代码,但我不想显示名为“ booksList”的节点。 i need your help about the solution i delete the XmlArrayItem tag but not work properly. 我需要您提供有关解决方案的帮助,我删除了XmlArrayItem标记,但无法正常工作。

i took the example from the following post : 我从以下帖子中获取了示例

You cannot serialize class from your question using standard serialization tools so that it will have entries on the same level as node. 您不能使用标准序列化工具序列化问题中的类,这样它的条目将与节点处于同一级别。 When class saved with standard serialization tools list of your nodes will always be nested into separate array node that will be on the same level as node. 当使用标准序列化工具保存的类时,您的节点列表将始终嵌套在与节点处于同一级别的单独的数组节点中。 Same concerns records array field on book class. 同样关注在书类上记录数组字段。 To generate XML output that you want to - with nodes on same level as node - you will have to implement IXmlSerializable interface in your books class for custom serialization. 要生成所需的XML输出(节点与节点处于同一级别),您必须在books类中实现IXmlSerializable接口以进行自定义序列化。 To see examples of IXmlSerializable implementation visit these links: StackOverflow answer, CodeProject article. 要查看IXmlSerializable实现的示例,请访问以下链接:StackOverflow答案,CodeProject文章。 Another solution will be - as stated user Alexandr in comment to my answer - to inherit your books class from List type and to have on your book class field records of class type that is inherited from List type. 另一个解决方案是-如用户Alexandr在对我的回答的评论中所述-从List类型继承您的书类,并在您的书类上具有从List类型继承的类类型的字段记录。

[XmlRoot("books")]
public class books
{
    [XmlElement("bookNum")]
    public int bookNum { get; set; }

    [XmlRoot("book")]
    public class book
    {
        [XmlElement("name")]
        public string name { get; set; }

        [XmlRoot("record")]
        public class record
        {
            [XmlElement("borrowDate")]
            public string borrowDate { get; set; }

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

        [XmlArray("borrowRecords")]
        [XmlArrayItem("record")]
        public record[] records { get; set; }
    }

    [XmlArray("booksList")]
    [XmlArrayItem("book")]
    public book[] books { get; set; }
}
<books>
    <bookNum>2</bookNum>
    <booksList>
        <book>
            <name>Book 1</name>
            <borrowRecords>
                <record>
                    <borrowDate>2013-1-3</borrowDate>
                    <returnDate>2013-1-5</returnDate>
                </record>
                <record>            
                    <borrowDate>2013-2-3</borrowDate>
                    <returnDate>2013-4-5</returnDate>
                </record>
            </borrowRecords>
        </book>
        <book>
            <name>Book 2</name>
            <borrowRecords>
                <record>
                    <borrowDate>2013-1-3</borrowDate>
                    <returnDate>2013-1-5</returnDate>
                </record>
                <record>            
                    <borrowDate>2013-2-3</borrowDate>
                    <returnDate>2013-4-5</returnDate>
                </record>
            </borrowRecords>
        </book>
    </booksList>
</books>

if i use the tag "XmlIgnore" like Ed Plunkett says work but i delete all the data inside the array, i want the data but not the "booksList" node. 如果我像Ed Plunkett所说的那样使用标签“ XmlIgnore”工作,但是我删除了数组内的所有数据,则我需要数据,而不是“ booksList”节点。

like this: 像这样:

<books>
    <bookNum>2</bookNum>
    <book>
        <name>Book 1</name>
        <borrowRecords>
            <record>
                <borrowDate>2013-1-3</borrowDate>
                <returnDate>2013-1-5</returnDate>
            </record>
            <record>
                <borrowDate>2013-2-3</borrowDate>
                <returnDate>2013-4-5</returnDate>
            </record>
        </borrowRecords>
    </book>
    <book>
        <name>Book 2</name>
        <borrowRecords>
            <record>
                <borrowDate>2013-1-3</borrowDate>
                <returnDate>2013-1-5</returnDate>
            </record>
            <record>
                <borrowDate>2013-2-3</borrowDate>
                <returnDate>2013-4-5</returnDate>
            </record>
        </borrowRecords>
    </book>
</books>

i use this example because the original data i can't show, but is the same problem i need to delete the "booksList" because is the standar i need to use. 我使用此示例是因为我无法显示原始数据,但是是我需要删除“ booksList”的相同问题,因为这是我需要使用的标准。

i found the solution, just change or add the [XmlArrayItem("book")] for [XmlElement("book")] in the cs class like this: 我找到了解决方案,只需在CS类中为[XmlElement(“ book”)]更改或添加[XmlArrayItem(“ book”)],如下所示:

[XmlRoot("books")]
public class books
{
    [XmlElement("bookNum")]
    public int bookNum { get; set; }

    [XmlRoot("book")]
    public class book
    {
        [XmlElement("name")]
        public string name { get; set; }

        [XmlRoot("record")]
        public class record
        {
            [XmlElement("borrowDate")]
            public string borrowDate { get; set; }

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

        [XmlElement("record")]
        public record[] records { get; set; }
    }

    [XmlElement("book")]
    public book[] books { get; set; }
}

and this gets the following xml file: 这将获得以下xml文件:

<books>
    <bookNum>2</bookNum>
    <book>
        <name>Book 1</name>
        <borrowRecords>
            <record>
                <borrowDate>2013-1-3</borrowDate>
                <returnDate>2013-1-5</returnDate>
            </record>
            <record>
                <borrowDate>2013-2-3</borrowDate>
                <returnDate>2013-4-5</returnDate>
            </record>
        </borrowRecords>
    </book>
    <book>
        <name>Book 2</name>
        <borrowRecords>
            <record>
                <borrowDate>2013-1-3</borrowDate>
                <returnDate>2013-1-5</returnDate>
            </record>
            <record>
                <borrowDate>2013-2-3</borrowDate>
                <returnDate>2013-4-5</returnDate>
            </record>
        </borrowRecords>
    </book>
</books>

without the node "" 没有节点“”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM