C# 自定义对象的xmlserializer
以下是我的xml文件的外观: 我试图使用xsd为我的对象生成类,但当我试图反序列化它时,不知何故它并没有起作用。我需要列作为字符串数组,我的类(对象)应该是什么,以便它可以反序列化xmlC# 自定义对象的xmlserializer,c#,xmlserializer,C#,Xmlserializer,以下是我的xml文件的外观: 我试图使用xsd为我的对象生成类,但当我试图反序列化它时,不知何故它并没有起作用。我需要列作为字符串数组,我的类(对象)应该是什么,以便它可以反序列化xml <ArrayOfDirective> <Directive> <TestCaseName>RunSqlCar</TestCaseName> <Action>IgnoreColumn</Action> <Columns> &l
<ArrayOfDirective>
<Directive>
<TestCaseName>RunSqlCar</TestCaseName>
<Action>IgnoreColumn</Action>
<Columns>
<ColumnName>value1</ColumnName>
<ColulmnName>value2</ColulmnName>
</Columns>
<Description>These columns never match becuase IDs are different always.</Description>
</Directive>
</ArrayOfDirective>
飞车
信号柱
价值1
价值2
这些列从不匹配,因为ID总是不同的。
错误:
读取c:\directions.xml时出错:xml文档(2,2)中有错误。使用XmlSerializer序列化的数据将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDirective xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Directives>
<Directive>
<TestCaseName>RunSqlCar</TestCaseName>
<Action>IgnoreColumn</Action>
<Columns>
<Column>
<ColumnName>value1</ColumnName>
</Column>
<Column>
<ColumnName>value2</ColumnName>
</Column>
</Columns>
<Description>These columns never match because IDs are different always.</Description>
</Directive>
</Directives>
</ArrayOfDirective>
飞车
信号柱
价值1
价值2
这些列从不匹配,因为ID总是不同的。
这些是序列化为上述XML的示例类
class Program
{
static void Main(string[] args)
{
ArrayOfDirective directives = new ArrayOfDirective();
Directive directive = new Directive("RunSqlCar", "IgnoreColumn",
"These columns never match because IDs are different always.");
directive.Columns.Add(new Column("value1"));
directive.Columns.Add(new Column("value2"));
directives.Directives.Add(directive);
XmlSerializer ser = new XmlSerializer(typeof(ArrayOfDirective));
using (StreamWriter sw = File.CreateText("c:\\directives_generated.xml"))
{
ser.Serialize(sw, directives);
}
}
}
[Serializable]
public class ArrayOfDirective
{
public List<Directive> Directives { get; set; }
public ArrayOfDirective()
{
Directives = new List<Directive>();
}
}
[Serializable]
public class Directive
{
public string TestCaseName { get; set; }
public string Action { get; set; }
public List<Column> Columns { get; set; }
public string Description { get; set; }
public Directive(string testCaseName, string action, string description)
{
TestCaseName = testCaseName;
Action = action;
Description = description;
Columns = new List<Column>();
}
public Directive()
{
}
}
[Serializable]
public class Column
{
public string ColumnName { get; set; }
public Column(string columnName)
{
ColumnName = columnName;
}
public Column()
{
}
}
类程序
{
静态void Main(字符串[]参数)
{
ArrayOfDirective指令=新的ArrayOfDirective();
指令指令=新指令(“RunSqlCar”、“IgnoreColumn”,
“这些列从不匹配,因为ID总是不同的。”);
指令.Columns.Add(新列(“值1”));
指令.Columns.Add(新列(“值2”));
指令。指令。添加(指令);
XmlSerializer ser=新的XmlSerializer(typeof(ArrayOfDirective));
使用(StreamWriter sw=File.CreateText(“c:\\directives\u generated.xml”))
{
序列序列化(软件、指令);
}
}
}
[可序列化]
公共类ArrayOfDirective
{
公共列表指令{get;set;}
公共阵列定向()
{
指令=新列表();
}
}
[可序列化]
公共类指令
{
公共字符串TestCaseName{get;set;}
公共字符串操作{get;set;}
公共列表列{get;set;}
公共字符串说明{get;set;}
公共指令(字符串testCaseName、字符串操作、字符串描述)
{
TestCaseName=TestCaseName;
行动=行动;
描述=描述;
Columns=新列表();
}
公共指令()
{
}
}
[可序列化]
公共类专栏
{
公共字符串ColumnName{get;set;}
公共列(字符串columnName)
{
ColumnName=ColumnName;
}
公共专栏()
{
}
}
请编辑您的问题,而不是对其进行评论。我的问题是正确的。首先,我想我没有添加那个部分。你们想看看xsd生成的类吗?,但不知怎的,它不起作用了
什么不起作用了?您遇到了什么错误?它引发了一个异常…“xml文件中存在错误”。我不想生成xml文件,而是使用XMLserializer.deserialize将xml读入对象列表