简体   繁体   English

c#XML序列化

[英]c# XML serialization

I'd like to serialize something like this, where there is a header and a body. 我想序列化这样的东西,其中有一个标题和一个正文。

The first part "galleryData" is the header The 2nd part is "imageData" - repeated for each image in gallery 第一部分“galleryData”是标题第二部分是“imageData” - 为图库中的每个图像重复

<galleryData>
    <title>some title</title>
    <uuid>32432322</uuid>
    <imagepath>some path</imagepath>
</galleryData>

<imageData>
    <title>title one</title>
    <category>nature</category>
    <description>blah blah</description>
</imageData>

<imageData>
     <title>title two</title>
     <category>nature</category>
     <description>blah blah</description> 
</imageData>

<imageData>
    <title>title three</title>
    <category>nature</category>
    <description>blah blah</description>
</imageData>

I see how to do it if I didn't need a header area. 如果我不需要标题区域,我会看到如何做到这一点。 I'm currently just using xmlwriter to create it, but I'd like to serialize the object out to xml instead. 我目前只是使用xmlwriter来创建它,但我想将对象序列化为xml。

You need a root in order to have valid XML. 您需要一个root才能拥有有效的XML。 Here's an example of how your model might look like: 以下是模型外观的示例:

public class ImageData
{
    [XmlElement("title")]
    public string Title { get; set; }
    [XmlElement("category")]
    public string Category { get; set; }
    [XmlElement("description")]
    public string Description { get; set; }
}

public class GalleryData
{
    [XmlElement("title")]
    public string Title { get; set; }
    [XmlElement("uuid")]
    public string UUID { get; set; }
    [XmlElement("imagepath")]
    public string ImagePath { get; set; }
}

public class MyData
{
    [XmlElement("galleryData")]
    public GalleryData GalleryData { get; set; }

    [XmlElement("imageData")]
    public ImageData[] ImageDatas { get; set; }
}

and then simply create an instance of this model and serialize it to a stream: 然后只需创建此模型的实例并将其序列化为流:

class Program
{
    static void Main()
    {
        var myData = new MyData
        {
            GalleryData = new GalleryData
            {
                Title = "some title",
                UUID = "32432322",
                ImagePath = "some path"
            },
            ImageDatas = new[]
            {
                new ImageData
                {
                    Title = "title one",
                    Category = "nature",
                    Description = "blah blah"
                },
                new ImageData
                {
                    Title = "title two",
                    Category = "nature",
                    Description = "blah blah"
                },
            }
        };

        var serializer = new XmlSerializer(myData.GetType());
        serializer.Serialize(Console.Out, myData);
    }
}

Given the way that XML serialization works, I do not believe the structure you are looking for will be possible from a straight Object -> XML structure as in your example you have more than one root node. 鉴于XML序列化的工作方式,我不相信您正在寻找的结构可能来自直接的Object - > XML结构,就像您的示例中有多个根节点一样。

If you had something where there was a container node, then individual ImageData elements within them, or a single over arching element to bundle them together you might be able to get by with it. 如果你有一个容器节点的东西,然后在它们中的单个ImageData元素,或者一个过度的拱形元素将它们捆绑在一起,你可能可以使用它。

If you are using XmlSerialization, you will need a root element - which will be the class that you serialize. 如果您使用的是XmlSerialization,则需要一个根元素 - 它将是您序列化的类。

Your galleryData and imageData will be object instance variables within the class used for your root element. 您的galleryData和imageData将是用于根元素的类中的对象实例变量。

您可以为标题创建一个对象,使用XmlSerializabe标记属性,并添加List类型的字段,以便标准xml序列化将序列化为标题的子元素

You can use something like the following to serialize a .NET object to an XML string: 您可以使用类似以下内容将.NET对象序列化为XML字符串:

    protected string ObjectToXml<T>(T obj)
    {
        var sw = new StringWriter();

        try
        {
            var mySerializer = new XmlSerializer(typeof(T));
            mySerializer.Serialize(sw, obj);
        }
        catch (Exception ex)
        {
           // Error logging here
        }

        return sw.ToString();
    }

You will need a root element for your XML document, and if your current model doesn't match the desired serialized form, then create and populate an appropriate data transfer object to do the serialisation job. 您将需要XML文档的根元素,如果当前模型与所需的序列化表单不匹配,则创建并填充相应的数据传输对象以执行序列化作业。

The structure you're showing is not valid XML because is containing more then one root node, so you can forget about XmlSerializer. 您显示的结构不是有效的XML,因为它包含多个根节点,因此您可以忘记XmlSerializer。 If you want to handle easily with such a xml similar structures I suggest Html Agility Pack 如果你想用这样的xml类似结构轻松处理,我建议使用Html Agility Pack

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

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