C# 使用XmlSerializer反序列化XML,其中父级和子级具有相同的标记
我在解析父节点和子节点具有相同标记名的XML字符串时遇到困难。显然,我可以用空字符串替换打开/关闭标记,并用下面的代码进行解析,但这并不优雅 我已经搜索并看到了关于如何使用XDocument实现这一点的答案,但我特别希望使用XmlSerializer实现这一点(如果可能的话) 下面是一个最小的、可复制的示例 XML示例:C# 使用XmlSerializer反序列化XML,其中父级和子级具有相同的标记,c#,xml,xml-parsing,C#,Xml,Xml Parsing,我在解析父节点和子节点具有相同标记名的XML字符串时遇到困难。显然,我可以用空字符串替换打开/关闭标记,并用下面的代码进行解析,但这并不优雅 我已经搜索并看到了关于如何使用XDocument实现这一点的答案,但我特别希望使用XmlSerializer实现这一点(如果可能的话) 下面是一个最小的、可复制的示例 XML示例: <AddJob> <AddJob RequestStatus="OK" RequestMessage="Job successfuly added [
<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>
呼叫代码:
var addedJobResponse = XmlHelper.Deserialize<AddJob>(exampleXml);
var addedJobResponse=XmlHelper.Deserialize(exampleXml);
您的数据模型与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; }
}
[XmlRoot(“AddJob”)]
公共类AddJob
{
[xmlement(ElementName=“AddJob”)]
公共列表AddJobs{get;set;}
}
公共类嵌套ddjob
{
[XmlAttribute]
公共字符串RequestStatus{get;set;}
[XmlAttribute]
公共字符串请求消息{get;set;}
[XmlAttribute(“UUID”)]
公共字符串RipJobId{get;set;}
}
您的数据模型与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; }
}
[XmlRoot(“AddJob”)]
公共类AddJob
{
[xmlement(ElementName=“AddJob”)]
公共列表AddJobs{get;set;}
}
公共类嵌套ddjob
{
[XmlAttribute]
公共字符串RequestStatus{get;set;}
[XmlAttribute]
公共字符串请求消息{get;set;}
[XmlAttribute(“UUID”)]
公共字符串RipJobId{get;set;}
}
嵌套的AddJob元素看起来像一个数组,不能在根目录下有一个数组。因此,添加一个根类,如下面的代码:
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
命名空间控制台应用程序75
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
字符串xml=“”+
"" +
"";
根作业=XmlHelper.Deserialize(xml);
}
}
公共类XmlHelper
{
公共静态T反序列化(字符串xml)
{
var serializer=newxmlserializer(typeof(T));
T结果;
使用(var reader=newstringreader(xml))
{
结果=(T)序列化程序。反序列化(读取器);
}
返回结果;
}
}
[XmlRoot(“AddJob”)]
公共类根
{
公共AddJob AddJob{get;set;}
}
公共类AddJob
{
[XmlAttribute]
公共字符串RequestStatus{get;set;}
[XmlAttribute]
公共字符串请求消息{get;set;}
[XmlAttribute(“UUID”)]
公共字符串RipJobId{get;set;}
}
}
嵌套的AddJob元素看起来像一个数组,不能在根目录下有一个数组。因此,添加一个根类,如下面的代码:
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
命名空间控制台应用程序75
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
字符串xml=“”+
"" +
"";
根作业=XmlHelper.Deserialize(xml);
}
}
公共类XmlHelper
{
公共静态T反序列化(字符串xml)
{
var serializer=newxmlserializer(typeof(T));
T结果;
使用(var reader=newstringreader(xml))
{
结果=(T)序列化程序。反序列化(读取器);
}
返回结果;
}
}
[XmlRoot(“AddJob”)]
公共类根
{
公共AddJob AddJob{get;set;}
}
公共类AddJob
{
[XmlAttribute]
公共字符串RequestStatus{get;set;}
[XmlAttribute]
公共字符串请求消息{get;set;}
[XmlAttribute(“UUID”)]
公共字符串RipJobId{get;set;}
}
}
虽然目前最多只有一个AddJob,但如果软件供应商决定返回多个AddJob,您的解决方案将是未来的证明。Kudos虽然目前最多只有一个AddJob,但如果软件供应商决定返回多个AddJob,您的解决方案将是未来的证明。荣誉