各位大侠:
我想把一个数据库文件转成一个XML文件。。。要求转换出的格式为这样子的:
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="终端接口XML Schema-2.0.xsd" SN="" Version="3.0" License="123">
<Events>
<!--采购入库,未绑定其他附加功能-->
<Event Name="PurchaseWareHouseIn" MainAction="WareHouseIn">
<ActionMaping>
<Action Name="WareHouseIn">
<ActionData>Code</ActionData>
<ActionData>CorpOrderID</ActionData>
<ActionData>Actor</ActionData>
<ActionData>ActDate</ActionData>
<ActionData>WrongCode</ActionData>
<ActionData>UpperCorpOrderID</ActionData>
<ActionData>FromCorpID</ActionData>
</Action>
</ActionMaping>
<DataMaping>
<MetaData Name="Code" Type="string" />
<MetaData Name="CorpOrderID" Type="string" />
<MetaData Name="Actor" Type="string" />
<MetaData Name="ActDate" Type="Date" />
<MetaData Name="WrongCode" Type="Boolean" />
<MetaData Name="UpperCorpOrderID" Type="string" />
<MetaData Name="FromCorpID" Type="string" />
</DataMaping>
<DataField>
<Data Code="48913645" CorpOrderID="201408242001" Actor="1111" ActDate="2014-08-24 05:34:09" WrongCode="false" />
<Data Code="48913646" CorpOrderID="201408242001" Actor="1111" ActDate="2014-08-24 05:34:09" WrongCode="false" />
<Data Code="48913647" CorpOrderID="201408242001" Actor="1111" ActDate="2014-08-24 05:34:09" WrongCode="false" />
<Data Code="48913648" CorpOrderID="201408242001" Actor="1111" ActDate="2014-08-24 05:34:09" WrongCode="false" />
<Data Code="48913649" CorpOrderID="201408242001" Actor="1111" ActDate="2014-08-24 05:34:09" WrongCode="false" />
<Data Code="48913641" CorpOrderID="201408242001" Actor="1111" ActDate="2014-08-24 05:34:09" WrongCode="false" />
</DataField>
</Event>
</Events>
</Document>
可是我通过Dataset的WirteXML直接转出来不是这种格式。。。本人刚做这一块,不太懂,有没有大侠能给指点一下啊。。。
我转换出来是这样的?
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<ScanBarCode>
<ID>641</ID>
<ScanBarID>KB3239040V11DC0270</ScanBarID>
<ScanOrderTime>2013-12-20T08:29:15+08:00</ScanOrderTime>
<ScanUser>admin</ScanUser>
<ScanMFlag>1</ScanMFlag>
<ScanRight>1</ScanRight>
<ScanBarM>KB3239040V11DC0270</ScanBarM>
<OrderID>0000029061</OrderID>
<OrderType>0</OrderType>
<Remark>test</Remark>
</ScanBarCode>
<ScanBarCode>
<ID>642</ID>
<ScanBarID>KH3042013V11PTDC0319</ScanBarID>
<ScanOrderTime>2013-12-20T08:29:20+08:00</ScanOrderTime>
<ScanUser>admin</ScanUser>
<ScanMFlag>0</ScanMFlag>
<ScanRight>1</ScanRight>
<ScanBarM>KB3239040V11DC0270</ScanBarM>
<OrderID>0000029061</OrderID>
<OrderType>0</OrderType>
<Remark>test</Remark>
</ScanBarCode>
现在怎么样转换成上面的那种格式?谢谢各位了!!!
------解决思路----------------------
遍历datarow,是用linq to xml(XElement) 自定义节点名称并取值保存。
------解决思路----------------------
只能通过遍历去做。
如果你要考虑灵活性,就把遍历做的好一点,通过配置xml模板生成相关数据。后期维护只需要维护XML模板就行了。
------解决思路----------------------
XDocument doc = new XDocument();
XElement root = new XElement("Document");
XElement child0 = new XElement("Events");
root.Add(child0);
XElement child = new XElement("Event",new XAttribute("Name", "PurchaseWareHouseIn"),new XAttribute("MainAction", "WareHouseIn"));
root.Add(child);
XElement member = new XElement("ActionMaping");
child.Add(member);
XElement member1 = new XElement("Action");
member.Add(member1);
XElement info = new XElement("ActionData", "Code");
member1.Add(info);
XElement info1 = new XElement("ActionData", "CorpOrderID");
member1.Add(info1);
XElement info2 = new XElement("ActionData", "Actor");
member1.Add(info2);
XElement info3 = new XElement("ActionData", "ActDate");
member1.Add(info3);
XElement member2 = new XElement("DataMaping");
child.Add(member2);
XElement info11 = new XElement("MetaData", new XAttribute("Name", "Code"), new XAttribute("Type", "string"));
member2.Add(info11);
XElement info12 = new XElement("MetaData", new XAttribute("Name", "CorpOrderID"), new XAttribute("Type", "string"));
member2.Add(info12);
doc = new XDocument(root);
doc.Save(Application.StartupPath + "\\Test1.xml");