如何将包含嵌套元素的基于模式的xml文件读入表类型对象?
按照本文的思路,我正在寻找一种快速、简单的方法,以获取一个仔细呈现的XML模式和基于该模式构建的一组XML数据实例,并将它们快速读入一些类似于如何将包含嵌套元素的基于模式的xml文件读入表类型对象?,xml,c#-4.0,dataset,xsd,xml-parsing,Xml,C# 4.0,Dataset,Xsd,Xml Parsing,按照本文的思路,我正在寻找一种快速、简单的方法,以获取一个仔细呈现的XML模式和基于该模式构建的一组XML数据实例,并将它们快速读入一些类似于数据集的对象中。(可以是具有多个表、行和列的任何对象,实际上,只要我可以在网格中显示它,添加一些列,进行一些更改,并在与它进行聚合交互时将其推送到粒度对象或数据库中。)我本以为我会使用它来完成此操作,但它的表创建非常不智能,也许是因为我在xsd中使用全局声明/可重用的类型?还是因为我没有使用正确的参数实现 下面是详细的示例代码。但要解决这个问题:XmlRe
数据集的对象中。(可以是具有多个表、行和列的任何对象,实际上,只要我可以在网格中显示它,添加一些列,进行一些更改,并在与它进行聚合交互时将其推送到粒度对象或数据库中。)我本以为我会使用它来完成此操作,但它的表创建非常不智能,也许是因为我在xsd中使用全局声明/可重用的类型?还是因为我没有使用正确的参数实现
下面是详细的示例代码。但要解决这个问题:XmlReader
似乎没有任何工具来解释模式的关系性质;子元素必须链接到表结构中的父元素才能有意义,但结果输出不会这样做。读者也无法理解列表只是一个表的集合,设置列表是为了避免冲突的、不相关的兄弟(尽管示例代码中没有)
如果我希望主要以批量方式处理这些数据(实际上不需要对象),那么在充分尊重xsd中定义并在xml中执行的层次结构的同时,将其转换为C#的最佳实践是什么?最少的代码行方法是什么
还有一点需要注意的是:我没有嫁给XmlReader,并且确实调查过LINQtoXML和xsd.exe对象生成器,但这两种方法都意味着编写的代码(或清理的代码)要比我想象的要多,而不是使用严格、可读的xsd。几天前,我以为这将是一个快速的转变
示例架构,另存为FooSchema.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
targetNamespace="http://tempuri.org/FooSchema"
elementFormDefault="qualified"
xmlns="http://tempuri.org/FooSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="FooNoteType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="dateStamp" type="xs:dateTime" use="optional" />
<xs:attribute name="isBar" type="xs:boolean" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="FooType">
<xs:sequence>
<xs:element name="stuff" type="xs:string" />
<xs:element name="nonsense" type="xs:string" minOccurs="0" />
<xs:element name="note" type="FooNoteType" minOccurs="0" />
</xs:sequence>
<xs:attribute name="isBar" type="xs:boolean" use="required" />
</xs:complexType>
<xs:complexType name="FooListType">
<xs:sequence>
<xs:element name ="foo" type="FooType" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:element name="fullaFoo">
<xs:complexType>
<xs:sequence>
<xs:element name="foos" type="FooListType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
示例实例FooData.xml——注意嵌套的注释
<?xml version="1.0" encoding="utf-8" ?>
<fullaFoo xmlns="http://tempuri.org/FooSchema">
<foos>
<foo isBar="false">
<stuff>first</stuff>
<nonsense>item</nonsense>
<note>This is the first note.</note>
</foo>
<foo isBar="true">
<stuff>things</stuff>
<nonsense>other things</nonsense>
</foo>
<foo isBar="false">
<stuff>yet more information</stuff>
<nonsense>sound, fury, etc.</nonsense>
</foo>
<foo isBar="true">
<stuff>yes please</stuff>
<nonsense>no thank you</nonsense>
<note dateStamp="2012-02-12T00:00:00" isBar="false">RE good manners</note>
</foo>
<foo isBar="false">
<stuff>last</stuff>
<nonsense>item</nonsense>
</foo>
</foos>
</fullaFoo>
第一
项目
这是第一个音符。
东西
其他事情
更多信息
喧闹、愤怒等。
好的
不,谢谢
你很有礼貌
最后的
项目
基本预期C#变换
void Test_ReadXmlToDataset()
{
string pathRoot = @"C:\SomePath\";
string pathSchema = pathRoot + @"FooSchema.xsd";
string pathData = pathRoot + @"FooData.xml";
DataSet dsTest = new DataSet("testXml");
dsTest.ReadXmlSchema(pathSchema);
dsTest.ReadXml(pathData);
//rubber would meet road here...
DisplayTableStructure(dsTest);
}
void DisplayTableStructure(DataSet dataSet)
{
Console.WriteLine("\r\nTable structure \r\n");
Console.WriteLine("Tables count=" + dataSet.Tables.Count.ToString());
for (int i = 0; i < dataSet.Tables.Count; i++)
{
Console.WriteLine("\tTableName='" + dataSet.Tables[i].TableName + "'.");
Console.WriteLine("\tColumns count=" + dataSet.Tables[i].Columns.Count.ToString());
for (int j = 0; j < dataSet.Tables[i].Columns.Count; j++)
{
Console.WriteLine("\t\tColumnName='" +
dataSet.Tables[i].Columns[j].ColumnName + "', type = "
+ dataSet.Tables[i].Columns[j].DataType.ToString());
}
}
Console.ReadLine();
}
无效测试\u ReadXmlToDataset()
{
字符串pathRoot=@“C:\SomePath\”;
字符串pathSchema=pathRoot+@“FooSchema.xsd”;
字符串pathData=pathRoot+@“FooData.xml”;
数据集dsTest=新数据集(“testXml”);
ReadXmlSchema(pathSchema);
ReadXml(路径数据);
//橡胶会在这里遇到道路。。。
显示表结构(dsTest);
}
void DisplayTableStructure(数据集)
{
Console.WriteLine(“\r\n表格结构\r\n”);
Console.WriteLine(“Tables count=“+dataSet.Tables.count.ToString());
对于(int i=0;i