使用XDocument将不同类型的多个对象序列化为单个XML

时间:2022-10-27 09:46:20

I am trying to serialize a list of objects into XML using XDocument as the base. The purpose of this is to log request data inside a WCF service.

我试图使用XDocument作为基础将对象列表序列化为XML。这样做的目的是在WCF服务中记录请求数据。

My question is how can I take a variable list of objects and convert them into an XDocument? All the examples I could find were how to do this for a single object and not for multiple objects of different complex types.

我的问题是如何获取对象的变量列表并将它们转换为XDocument?我能找到的所有例子都是如何为单个对象执行此操作,而不是为不同复杂类型的多个对象执行此操作。

My desired output is something like this:

我想要的输出是这样的:

<rootElementName>
    <CustomObjectType1>
        <SubData1>Test</SubData1>
        <SubData2>2014-12-22T16:33:00</SubData2>
    </CustomObjectType1>
    <CustomObjectType2>
        <SubData3>123456</SubDataType2>
    </CustomObjectType2>
</rootElementName>

Originally I pieced this together from other * examples, but I want to use XDocument instead.

最初我将其与其他*示例拼凑在一起,但我想使用XDocument。

private static XmlDocument CreateXmlFromObjects(string rootElementName, params object[] inputs)
{
    var doc = new XmlDocument();
    var root = doc.AppendChild(doc.CreateElement(rootElementName));

    doc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    doc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");

    foreach(var input in inputs)
    {
        SerializeAppend(doc, input);
    }

    return doc;
}

private static void SerializeAppend(XmlNode parentNode, object obj)
{
    XPathNavigator nav = parentNode.CreateNavigator();
    using (var writer = nav.AppendChild())
    {
        var serializer = new XmlSerializer(obj.GetType());
        writer.WriteWhitespace("");
        serializer.Serialize(writer, obj);
        writer.Close();
    }
}

I tried using the code example from here and here, but was getting errors.

我尝试使用此处和此处的代码示例,但是遇到了错误。

I tried this

我试过这个

private static XDocument CreateXmlFromObjects(string rootElementName, params object[] inputs)
{
    var doc = new XDocument();
    doc.Add(new XElement(rootElementName, inputs.Select(x => SerializeAppend2(x))));

    return doc;
}

private static XDocument SerializeAppend2(object obj)
{
    var document = new XDocument();
    using (var writer = document.CreateWriter())
    {
        var serializer = new XmlSerializer(obj.GetType());
        serializer.Serialize(writer, obj);
    }

    return document;
}

1 个解决方案

#1


4  

It looks like you are almost there with your attempt, below should generate your desired output. Basically the difference is instead of having many XmlWriter and XDocument for each sub object, you only need one.

看起来你几乎在尝试,下面应该生成你想要的输出。基本上差异在于每个子对象都有许多XmlWriter和XDocument,而只需要一个。

private static XDocument CreateXmlFromObjects(string rootElementName, params object[] inputs)
{
    var doc = new XDocument();
    using (XmlWriter writer = doc.CreateWriter())
    {
        writer.WriteStartElement(rootElementName);                
        foreach (var input in inputs)
            new XmlSerializer(input.GetType()).Serialize(writer, input);
        writer.WriteEndElement();
    }

    return doc;
}

#1


4  

It looks like you are almost there with your attempt, below should generate your desired output. Basically the difference is instead of having many XmlWriter and XDocument for each sub object, you only need one.

看起来你几乎在尝试,下面应该生成你想要的输出。基本上差异在于每个子对象都有许多XmlWriter和XDocument,而只需要一个。

private static XDocument CreateXmlFromObjects(string rootElementName, params object[] inputs)
{
    var doc = new XDocument();
    using (XmlWriter writer = doc.CreateWriter())
    {
        writer.WriteStartElement(rootElementName);                
        foreach (var input in inputs)
            new XmlSerializer(input.GetType()).Serialize(writer, input);
        writer.WriteEndElement();
    }

    return doc;
}