C# 如何反序列化包含列表的xml文件?
我正在尝试反序列化包含id列表的xml,但它给了我错误“无法生成临时类(结果=1)”,请帮助我。 以下是我的xml文件格式:C# 如何反序列化包含列表的xml文件?,c#,c#-4.0,c#-3.0,C#,C# 4.0,C# 3.0,我正在尝试反序列化包含id列表的xml,但它给了我错误“无法生成临时类(结果=1)”,请帮助我。 以下是我的xml文件格式: <?xml version="1.0" encoding="UTF-8"?> <identifiers> <Module Name="Doors_Module1" Path="Doors_Module1 "> <id value="16"/> <id value="15"/> <i
<?xml version="1.0" encoding="UTF-8"?>
<identifiers>
<Module Name="Doors_Module1" Path="Doors_Module1 ">
<id value="16"/>
<id value="15"/>
<id value="14"/>
<id value="13"/>
<id value="12"/>
<id value="11"/>
<id value="10"/>
<id value="9"/>
<id value="17"/>
<id value="8"/>
<id value="7"/>
<id value="6"/>
<id value="5"/>
<id value="4"/>
<id value="3"/>
<id value="2"/>
<id value="1"/>
</Module>
</identifiers>
下面是我反序列化该xml的类:
public class HelperAllIdentifiers
{
[Serializable, XmlRoot("identifiers")]
public class identifiers
{
public Module Module { get; set; }
}
[XmlRoot("Module")]
public class Module
{
[XmlAttribute("Name")]
public string Name
{
get;
set;
}
[XmlArrayItem("id", Type = typeof(Attribute))]
public List<IdValue> FieldList;// { get; set; }
public Attribute[] ids { get; set; }
}
[XmlRoot("id")]
public class IdValue
{
[XmlAttribute("value")]// Type=typeof(Attribute))]
public string Value { get; set; }
}
}
公共类HelperAllIdentifiers
{
[可序列化,XmlRoot(“标识符”)]
公共类标识符
{
公共模块模块{get;set;}
}
[XmlRoot(“模块”)]
公共类模块
{
[XmlAttribute(“名称”)]
公共字符串名
{
得到;
设置
}
[XmlArrayItem(“id”,Type=typeof(属性))]
公共列表字段列表;//{get;set;}
公共属性[]id{get;set;}
}
[XmlRoot(“id”)]
公共类IdValue
{
[xmldattribute(“value”)]//Type=typeof(属性))]
公共字符串值{get;set;}
}
}
高级版谢谢。以下代码对我有用:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
namespace ConsoleWinForm
{
[XmlRoot(ElementName = "id")]
public class Id
{
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Module")]
public class Module
{
[XmlElement(ElementName = "id")]
public List<Id> Id { get; set; }
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Path")]
public string Path { get; set; }
}
[XmlRoot(ElementName = "identifiers")]
public class Identifiers
{
[XmlElement(ElementName = "Module")]
public Module Module { get; set; }
}
public class Program
{
public static void Main()
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<identifiers>
<Module Name=""Doors_Module1"" Path=""Doors_Module1 "">
<id value=""16""/>
<id value=""15""/>
<id value=""14""/>
<id value=""13""/>
<id value=""12""/>
<id value=""11""/>
<id value=""10""/>
<id value=""9""/>
<id value=""17""/>
<id value=""8""/>
<id value=""7""/>
<id value=""6""/>
<id value=""5""/>
<id value=""4""/>
<id value=""3""/>
<id value=""2""/>
<id value=""1""/>
</Module>
</identifiers>";
using (var rdr = new StringReader(xml))
{
var srlzr = new XmlSerializer(typeof(Identifiers));
var result = srlzr.Deserialize(rdr) as Identifiers;
}
}
}
}
使用系统;
使用System.Xml.Serialization;
使用System.Collections.Generic;
使用System.IO;
命名空间控制台信息
{
[XmlRoot(ElementName=“id”)]
公共类Id
{
[XmlAttribute(AttributeName=“value”)]
公共字符串值{get;set;}
}
[XmlRoot(ElementName=“Module”)]
公共类模块
{
[xmlement(ElementName=“id”)]
公共列表Id{get;set;}
[XmlAttribute(AttributeName=“Name”)]
公共字符串名称{get;set;}
[XmlAttribute(AttributeName=“Path”)]
公共字符串路径{get;set;}
}
[XmlRoot(ElementName=“标识符”)]
公共类标识符
{
[XmlElement(ElementName=“模块”)]
公共模块模块{get;set;}
}
公共课程
{
公共静态void Main()
{
字符串xml=@“
";
使用(var rdr=newstringreader(xml))
{
var srlzr=新的XmlSerializer(typeof(标识符));
var result=srlzr.反序列化(rdr)作为标识符;
}
}
}
}
以下代码对我有效:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
namespace ConsoleWinForm
{
[XmlRoot(ElementName = "id")]
public class Id
{
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Module")]
public class Module
{
[XmlElement(ElementName = "id")]
public List<Id> Id { get; set; }
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Path")]
public string Path { get; set; }
}
[XmlRoot(ElementName = "identifiers")]
public class Identifiers
{
[XmlElement(ElementName = "Module")]
public Module Module { get; set; }
}
public class Program
{
public static void Main()
{
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<identifiers>
<Module Name=""Doors_Module1"" Path=""Doors_Module1 "">
<id value=""16""/>
<id value=""15""/>
<id value=""14""/>
<id value=""13""/>
<id value=""12""/>
<id value=""11""/>
<id value=""10""/>
<id value=""9""/>
<id value=""17""/>
<id value=""8""/>
<id value=""7""/>
<id value=""6""/>
<id value=""5""/>
<id value=""4""/>
<id value=""3""/>
<id value=""2""/>
<id value=""1""/>
</Module>
</identifiers>";
using (var rdr = new StringReader(xml))
{
var srlzr = new XmlSerializer(typeof(Identifiers));
var result = srlzr.Deserialize(rdr) as Identifiers;
}
}
}
}
使用系统;
使用System.Xml.Serialization;
使用System.Collections.Generic;
使用System.IO;
命名空间控制台信息
{
[XmlRoot(ElementName=“id”)]
公共类Id
{
[XmlAttribute(AttributeName=“value”)]
公共字符串值{get;set;}
}
[XmlRoot(ElementName=“Module”)]
公共类模块
{
[xmlement(ElementName=“id”)]
公共列表Id{get;set;}
[XmlAttribute(AttributeName=“Name”)]
公共字符串名称{get;set;}
[XmlAttribute(AttributeName=“Path”)]
公共字符串路径{get;set;}
}
[XmlRoot(ElementName=“标识符”)]
公共类标识符
{
[XmlElement(ElementName=“模块”)]
公共模块模块{get;set;}
}
公共课程
{
公共静态void Main()
{
字符串xml=@“
";
使用(var rdr=newstringreader(xml))
{
var srlzr=新的XmlSerializer(typeof(标识符));
var result=srlzr.反序列化(rdr)作为标识符;
}
}
}
}
当我必须将Xml序列化为C#类时,我会这样做。感谢您的快速回复prateek,但现在我得到了错误:未处理的异常:System.InvalidOperationException:XML文档(2,2)中有一个错误。-->System.InvalidOperationException:不是预期的。以下是当我必须将Xml序列化到C#类时所做的操作。感谢您的快速回复prateek,但现在我得到了错误:未处理的异常:System.InvalidOperationException:XML文档(2,2)中有一个错误。-->System.InvalidOperationException:不是预期的。我不确定这是否是根本原因,但您的模块类与XML文件的内容不同:没有响应属性“Path”的属性。实际上,我的结果中不需要属性“Path”。这就是为什么我没有在Classic中使用该属性的原因。我不确定这是否是根本原因,但您的模块类与XML文件的内容不同:没有响应属性“Path”的属性。实际上,我不需要在结果中使用属性“Path”。这就是为什么我没有在我的类中使用该属性