从数据集创建XML文件? [英] Create an XML file from a dataset?
问题描述
我一直在研究一个需要非常具体的XML文件的项目。好像我没有得到正确的输出。
我使用ds.READXML(文件名)
从原始XML文件创建了一个数据集然后我只使用XML Schema编写了WRITEXML(filename,writeschema)
现在我使用该模式构建空白数据集,并在正确的表中填写列的值。这一切都很好。当我用加载的值写出Datset时,格式错误。
原始XML:
I've been working on a project that requires a very specific XML file. It seems that I'm not getting the correct output.
I have created a dataset from the original XML file using ds.READXML(filename)
then I wrote out just the XML Schema using the WRITEXML(filename,writeschema)
Now I use that schema to build blank dataset and the I fill in my values for the columns within the correct tables. This all works just fine. Its when I write out the Datset with the loaded values I get the wrong format.
Original XML:
<dsData Layout="1.lyt" Start="" Test="YES" Prompt="NO" AutoClose="YES">
<Table1 ID="1">
<Col1>FIRST</Col1>
<Services Delivery="OFF"></Services>
</Table1>
</dsData>
XML的架构:
Schema for the XML:
<?xml version="1.0" standalone="yes"?>
<xs:schema id="dsData" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="Table2" maxOccurs="unbounded" minOccurs="0" >
<xs:complexType>
<xs:sequence>
<xs:element name="Col1" type="xs:string" minOccurs="0" />
<xs:element name="Services" maxOccurs="unbounded" minOccurs="0" >
<xs:complexType>
<xs:attribute name="Delivery" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="ID" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Layout" type="xs:string" />
<xs:attribute name="Prompt" type="xs:string" />
<xs:attribute name="AbortOnError" type="xs:string" />
<xs:attribute name="Test" type="xs:string" />
<xs:attribute name="Start" type="xs:string" />
<xs:attribute name="OutputFile" type="xs:string" />
<xs:attribute name="SkipUnverified" type="xs:string" />
<xs:attribute name="AutoClose" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
创建最终输出的代码:
Code to create final output:
Dim xSettings As XmlWriterSettings = New XmlWriterSettings
xSettings.OmitXmlDeclaration = True
xSettings.Indent = True
Dim xmlSW As XmlWriter = XmlWriter.Create("F:\TestDaz.xml", xSettings)
dsData.WriteXml(xmlSW)
实际输出:
Actual output:
<dsData>
<Table1 Layout="1.lyt" Prompt="No" AbortOnError="No" Test="No" SkipUnverified="No" AutoClose="Yes" />
<Table2 ID="1">
<Col1>FIRST</Col1>
</Table2>
</dsData>
我想不输出dsData元素,我希望Table1作为根元素。我在Service元素(表)中没有任何数据,所以这里没有输出样本,但在Table2中有2次出现的服务。
什么我错过了吗?如何从dsData.writexml写入输出以符合原始格式?
I want to not output dsData element and I want the Table1 as the root element. I don't have any data in the Service element(table) so that is not output in sample here but with in Table2 there are 2 occurrences of Services.
What am I missing? How do I get the output from dsData.writexml write to conform to the original format?
推荐答案
在整天搜索网络和试错后我发现我有2个问题。第一个是虽然数据集确实已经定义了关系,但由于XSD文件的结构,我没有填充子表中的键。
每个子表都有一个必须填充的(Tablename)_ID的隐藏列。这使得嵌套工作。
第二个问题是内部枚举器/序列化器如何将数据集转换为XML。它就是这样。
所以输出到磁盘文件,相当邋but但有效;
Well after entire day searching the web and trial and error I found that I had 2 problems. The first is that although the dataset did indeed have the Relations defined, due the the structure of the XSD file, I was not populating the keys within the sub tables.
Each sub table has a hidden column for (Tablename)_ID that must be populated. That got the nesting working.
The second problem his how the Dataset is converted to XML by internal enumerator/serializer. It is what it is.
So to output to disk file, rather sloppy but works;
Dim doc As XmlDocument = New XmlDocument
doc.LoadXml(dsDazz.GetXml) 'Get xml of dataset loaded into an xdoc
Dim doc2 As XmlDocument = New XmlDocument
doc2.LoadXml(doc.FirstChild.InnerXml) 'now load into new xdoc the first inner child
Dim xSettings As XmlWriterSettings = New XmlWriterSettings 'make a clean output
xSettings.Indent = True
xSettings.OmitXmlDeclaration = True
Dim xWriter As XmlWriter = XmlWriter.Create("F:\testXml2.xml", xSettings)
doc2.Save(xWriter) 'put the XML to a file
最终输出符合要求,否则为newdataset 根元素和所有嵌套元素都在正确的位置。
The final output is as desired, No "newdataset" root element and all nested Elements are in the right place.
这篇关于从数据集创建XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!