使用XmlSerializer反序列化XML,其中父级和子级具有相同的标签

时间:2018-10-25 13:39:05

标签: c# xml xml-parsing

我在解析父节点和子节点具有相同标签名称的XML字符串时遇到困难。显然,我可以用空字符串替换open / close标记,并使用下面的代码进行解析,但这并不完美。

我进行了搜索,发现有一些关于如何使用XDocument执行此操作的答案,但是我特别想使用XmlSerializer(如果可能)执行此操作。

下面是一个最小的,可复制的示例。

示例XML:

<AddJob>
    <AddJob RequestStatus="OK" RequestMessage="Job successfuly added [testPrintServer.tif, PES_Carpet_16C_76.2 x 50.8 dpi_170517_Normal]" UUID="74ad5971-7baf-49ce-b85b-ee08188d5721" />
</AddJob>

解析代码:

public class XmlHelper
{
    public static T Deserialize<T>(string xml)
    {
        var serializer = new XmlSerializer(typeof(T));

        T result;
        using (var reader = new StringReader(xml))
        {
            result = (T)serializer.Deserialize(reader);
        }
        return result;
    }
}

数据模型:

[XmlRoot("AddJob")]
public class AddJob
{
    [XmlAttribute]
    public string RequestStatus { get; set; }

    [XmlAttribute]
    public string RequestMessage { get; set; }

    [XmlAttribute("UUID")]
    public string RipJobId { get; set; }
}

呼叫代码:

var addedJobResponse = XmlHelper.Deserialize<AddJob>(exampleXml);

2 个答案:

答案 0 :(得分:1)

您的数据模型与您的xml结构不匹配。

请使用类似的内容

[XmlRoot("AddJob")]
public class AddJob
{
    [XmlElement(ElementName = "AddJob")]
    public List<NestedAddJob> AddJobs { get; set; }
}

public class NestedAddJob
{
    [XmlAttribute]
    public string RequestStatus { get; set; }

    [XmlAttribute]
    public string RequestMessage { get; set; }

    [XmlAttribute("UUID")]
    public string RipJobId { get; set; }
}

答案 1 :(得分:1)

嵌套的AddJob元素看起来像一个数组,并且根目录不能有数组。因此,添加一个类似于以下代码的Root类:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication75
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
           string xml = "<AddJob>" +
              "<AddJob RequestStatus=\"OK\" RequestMessage=\"Job successfuly added [testPrintServer.tif, PES_Carpet_16C_76.2 x 50.8 dpi_170517_Normal]\" UUID=\"74ad5971-7baf-49ce-b85b-ee08188d5721\" />" +
              "</AddJob>";
           Root job = XmlHelper.Deserialize<Root>(xml);

        }

    }
    public class XmlHelper
    {

        public static T Deserialize<T>(string xml)
        {
            var serializer = new XmlSerializer(typeof(T));

            T result;
            using (var reader = new StringReader(xml))
            {
                result = (T)serializer.Deserialize(reader);
            }
            return result;
        }
    }
    [XmlRoot("AddJob")]
    public class Root
    {
        public AddJob AddJob { get; set; }
    }
    public class AddJob
    {
        [XmlAttribute]
        public string RequestStatus { get; set; }

        [XmlAttribute]
        public string RequestMessage { get; set; }

        [XmlAttribute("UUID")]
        public string RipJobId { get; set; }
    }



}