如何从序列化的xml中删除root? [英] How do I remove root from my serialized xml?

查看:189
本文介绍了如何从序列化的xml中删除root?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个序列化的XML转换为Json(不确定我是否说得对)。它成功地进行了序列化,但我将把这个Json传递给web api,通过邮递员发布。不幸的是,我需要从生成的Json中删除root。

 {  root:{  ExtSerial  KDI2015050501 日期  02/01/2015  CustomerRefNbr  15-000001 描述  2015 FEBRUARY RENTAL 客户  TRDA0139 金额  482072.5800  AQ_Branch  KDI  AQ_COA  4100503000  LineSubAccount  OPMOPN000  LineTaxCategory   LineQuantity   1  LineUnitPrice  482072.580000  AQ_PostStatus  0   AQ_StatusDate  02/01 / 2015  DTS   02/01/2015  LineDescription 办公室租赁 - 零评级销售}} 



如果我从源XML中删除root,它会一直发出异常:

发生了'System.Xml.XmlException'类型的未处理异常System.Xml.dll

附加信息:有多个根元素。第2行,第2位。


这是我到目前为止的代码。我使用Linq库和JSON.NET进行序列化。

 XDocument xdoc = XDocument.Load( @  D:\Documents\Projects\HeXML\tae.xml); 
IEnumerable< XElement> Invoices = xdoc.Elements();

foreach var 发​​票 in 发​​票)
{
var json = JsonConvert.SerializeObject(Invoice);
Console.WriteLine(json);
}
Console.ReadLine();



我希望有人能帮帮我。



编辑:



这是XML

 <   root  >  
< ExtSerial > KDI2015050501 < / ExtSerial >
< 日期 > 02/01/2015 < < span class =code-leadattribute> / Date >
< CustomerRefNbr > 15-000001 < / CustomerRefNbr >
< 描述 > 2015 FEBRUARY RENTAL < / Description >
< 客户 > TRDA0139 < / Customer >
< 金额 > 482072.5800 < / Amount >
< AQ_Branch > KDI < / AQ_Branch >
< AQ_COA > 4100503000 < / AQ_COA >
< LineSubAccount > OPMOPN000 < / LineSubAccount >
< LineTaxCategory > 错误< / LineTaxCategory >
< LineQuantity > 1 < / LineQuantity >
< LineUnitPrice > 482072.580000 < / LineUnitPrice >
< AQ_PostStatus > 0 < / AQ_PostStatus >
< AQ_StatusDate > 02/01/2015 < / AQ_StatusD ate >
< DTS > 02/01/2015 < / DTS >
< LineDescription > 办公室租赁 - 零级销售< < span class =code-leadattribute> / LineDescription
>
< / root >



我只是希望将它序列化为此JSON:

 {  ExtSerial  KDI2015050501 日期  02/01/2015  CustomerRefNbr  15-000001 描述  2015 FEBRUARY RENTAL 客户  TRDA0139 金额  482072.5800  AQ_Branch  KDI   AQ_C OA  4100503000   LineSubAccount  OPMOPN000  LineTaxCategory  False  LineQuantity:< span class =code-string>  1  LineUnitPrice  482072.580000  AQ_PostStatus  0  AQ_StatusDate  02/01/2015  DTS  02/01/2015  LineDescription 办公室租赁 - 零级销售} 

解决方案

这是无稽之谈。根据定义,XML文档只有一个根。如果删除根,则下一级层次结构可以是多个层次结构中的一个元素。如果只有一个元素,删除root将为您提供有效的XML(或至少格式良好)。在您的示例中不是这种情况,因此它不能是XML。



-SA


如果要跳过根元素并仅获取子节点,请使用:

  var  invoices = xdoc.Root.Elements(); 





现在,您可以将其转换为json对象。



注意,使用你的代码你会得到这样的东西:

 {ExtSerial:KDI2015050501},{ 日期:02/01/2015},... 



但不是

 { ExtSerial:KDI2015050501,日期:02/01/2015,...} 





我是建议阅读:使用JSON数据 [ ^ ]


Hi,

I have a serialized XML that is converted to Json (Not sure if I said that right). It did the serialization successfully but I am going to be passing this Json to a web api to be posted via postman. Unfortunately, I need to get rid of root from the produced Json.

{"root":{"ExtSerial":"KDI2015050501","Date":"02/01/2015","CustomerRefNbr":"15-000001","Description":"2015 FEBRUARY RENTAL","Customer":"TRDA0139","Amount":"482072.5800","AQ_Branch":"KDI","AQ_COA":"4100503000","LineSubAccount":"OPMOPN000","LineTaxCategory":"False","LineQuantity":"1","LineUnitPrice":"482072.580000","AQ_PostStatus":"0","AQ_StatusDate":"02/01/2015","DTS":"02/01/2015","LineDescription":"Office Rental - Zero-Rated Sales"}}


If I remove root from the source XML it keeps sprouting an exception:
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: There are multiple root elements. Line 2, position 2.

Here is my code so far. I have used Linq library and JSON.NET for serialization.

XDocument xdoc = XDocument.Load(@"D:\Documents\Projects\HeXML\tae.xml");
IEnumerable<XElement> Invoices = xdoc.Elements();

foreach (var  Invoice in Invoices)
{
    var json = JsonConvert.SerializeObject(Invoice);
    Console.WriteLine(json);
}
Console.ReadLine();


I hope someone could help me.

Edit:

Here is the XML

<root>
	<ExtSerial>KDI2015050501</ExtSerial>
	<Date>02/01/2015</Date>
	<CustomerRefNbr>15-000001</CustomerRefNbr>
	<Description>2015 FEBRUARY RENTAL</Description>
	<Customer>TRDA0139</Customer>
	<Amount>482072.5800</Amount>
	<AQ_Branch>KDI</AQ_Branch>
	<AQ_COA>4100503000</AQ_COA>
	<LineSubAccount>OPMOPN000</LineSubAccount>
	<LineTaxCategory>False</LineTaxCategory>
	<LineQuantity>1</LineQuantity>
	<LineUnitPrice>482072.580000</LineUnitPrice>  
	<AQ_PostStatus>0</AQ_PostStatus>
	<AQ_StatusDate>02/01/2015</AQ_StatusDate>
	<DTS>02/01/2015</DTS>
	<LineDescription>Office Rental - Zero-Rated Sales</LineDescription>
</root>


I just want to have it serialized to this JSON:

{"ExtSerial":"KDI2015050501","Date":"02/01/2015","CustomerRefNbr":"15-000001","Description":"2015 FEBRUARY RENTAL","Customer":"TRDA0139","Amount":"482072.5800","AQ_Branch":"KDI","AQ_COA":"4100503000","LineSubAccount":"OPMOPN000","LineTaxCategory":"False","LineQuantity":"1","LineUnitPrice":"482072.580000","AQ_PostStatus":"0","AQ_StatusDate":"02/01/2015","DTS":"02/01/2015","LineDescription":"Office Rental - Zero-Rated Sales"}

解决方案

This is nonsense. XML document, by definition, has one and only one root. If you remove the root, the next level of hierarchy can be one element of more than one. If there is only one element, removing root will give you valid XML (or at least well-formed). This is not the case in your example, so it cannot be XML.

—SA


If you want to skip root element and get only child nodes, use this:

var invoices = xdoc.Root.Elements();



Now, you can "convert" it into json object.

Note, that using your code you'll get something like this:

{"ExtSerial":"KDI2015050501"},{"Date":"02/01/2015"},...


but not

{"ExtSerial":"KDI2015050501","Date":"02/01/2015",...}



I'd suggest to read this: Working with JSON Data[^]


这篇关于如何从序列化的xml中删除root?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆