C# 如何删除每个集合的父元素并在json中将其表示为数组
我有以下XML:C# 如何删除每个集合的父元素并在json中将其表示为数组,c#,xml,serialization,json.net,C#,Xml,Serialization,Json.net,我有以下XML: <Envelops xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding
<Envelops
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<BusinessPartners>
<CardCode>L10002</CardCode>
<BPAddresses>
<row>
<AddressName>Bill To</AddressName>
<AddressType>bo_BillTo</AddressType>
<BPCode>L10002</BPCode>
<U_WBCUSTADDID>84</U_WBCUSTADDID>
<RowNum>0</RowNum>
</row>
</BPAddresses>
<U_WBCUSTID>74</U_WBCUSTID>
<UploadURL>BusinessPartners('L10002')</UploadURL>
</BusinessPartners>
</Envelops>
我得到的JSON格式如下:
{
"CardCode": "L10002",
"BPAddresses": {
"row": {
"AddressName": "Bill To",
"AddressType": "bo_BillTo",
"BPCode": "L10002",
"U_WBCUSTADDID": "84",
"RowNum": "0"
}
},
"U_WBCUSTID": "74",
"UploadURL": "BusinessPartners('L10002')"
}
我希望实现的是以下格式:
{
"CardCode": "L10002",
"BPAddresses": [{
"AddressName": "Bill To",
"AddressType": "bo_BillTo",
"BPCode": "L10002",
"U_WBCUSTADDID": "84",
"RowNum": "0"
}],
"U_WBCUSTID": "74",
"UploadURL": "BusinessPartners('L10002')"
}
如何执行此操作?在使用
SerializeXmlNode
之前,您需要对XML进行一些操作,以获得所需的结果
json:Array=“true”
属性添加到BPAddresses
元素,以向json.Net发送信号,在转换为json时将该元素视为数组的一部分。(注意json:
属性前缀对应于http://james.newtonking.com/projects/json
XML名称空间。)行
元素的子元素直接移动到BPAddresses
中。如果有多个行
,则为每行创建其他BPAddresses
元素作为原始BPAddresses
元素的同级。(确保每个新的BPAddresses
元素也具有json:Array=“true”
属性。)foreach (XmlElement data2 in d1.GetElementsByTagName("BusinessPartners"))
{
XmlElement bpa = (XmlElement)data2.SelectSingleNode("BPAddresses");
XmlElement insertionPoint = bpa;
foreach (XmlElement row in bpa.GetElementsByTagName("row"))
{
// Create a new BPAddresses element and add an Array="true" attribute to it
// so JSON.Net will know to treat it as an array
XmlElement addr = d1.CreateElement("BPAddresses");
XmlAttribute attr = d1.CreateAttribute("Array", "http://james.newtonking.com/projects/json");
attr.Value = "true";
addr.Attributes.Append(attr);
// move all the children of the row element into the new BPAddresses element
foreach (XmlElement child in row.GetElementsByTagName("*").Cast<XmlElement>().ToList())
{
addr.AppendChild(child);
}
// insert the new element after the last BPAddresses element
data2.InsertAfter(addr, insertionPoint);
insertionPoint = addr;
}
// remove the original BPAddresses element (and its children)
data2.RemoveChild(bpa);
var requestBody = JsonConvert.SerializeXmlNode(data2, Formatting.Indented, true);
Console.WriteLine(requestBody);
}
foreach(d1.GetElementsByTagName(“业务伙伴”)中的XmlElement数据2)
{
XmlElement bpa=(XmlElement)数据2.选择SingleNode(“BPAddresses”);
XmlElement insertionPoint=bpa;
foreach(bpa.GetElementsByTagName(“行”)中的XmlElement行)
{
//创建一个新的BPAddresses元素并向其添加Array=“true”属性
//因此JSON.Net将知道如何将其视为一个数组
XmlElement addr=d1.CreateElement(“BPAddresses”);
XmlAttribute attr=d1.CreateAttribute(“数组”http://james.newtonking.com/projects/json");
attr.Value=“true”;
addr.Attributes.Append(attr);
//将行元素的所有子元素移动到新的BPAddresses元素中
foreach(第行中的XmlElement子级.GetElementsByTagName(“*”).Cast().ToList())
{
地址:附属儿童(儿童);
}
//在最后一个BPAddresses元素之后插入新元素
数据2.InsertAfter(addr,insertionPoint);
insertionPoint=addr;
}
//删除原始BPAddresses元素(及其子元素)
数据2.RemoveChild(bpa);
var requestBody=JsonConvert.SerializeXmlNode(data2,Formatting.Indented,true);
Console.WriteLine(请求主体);
}
Fiddle:在使用
SerializeXmlNode
之前,您需要对XML进行一些操作,以获得所需的结果
json:Array=“true”
属性添加到BPAddresses
元素,以向json.Net发送信号,在转换为json时将该元素视为数组的一部分。(注意json:
属性前缀对应于http://james.newtonking.com/projects/json
XML名称空间。)行
元素的子元素直接移动到BPAddresses
中。如果有多个行
,则为每行创建其他BPAddresses
元素作为原始BPAddresses
元素的同级。(确保每个新的BPAddresses
元素也具有json:Array=“true”
属性。)foreach (XmlElement data2 in d1.GetElementsByTagName("BusinessPartners"))
{
XmlElement bpa = (XmlElement)data2.SelectSingleNode("BPAddresses");
XmlElement insertionPoint = bpa;
foreach (XmlElement row in bpa.GetElementsByTagName("row"))
{
// Create a new BPAddresses element and add an Array="true" attribute to it
// so JSON.Net will know to treat it as an array
XmlElement addr = d1.CreateElement("BPAddresses");
XmlAttribute attr = d1.CreateAttribute("Array", "http://james.newtonking.com/projects/json");
attr.Value = "true";
addr.Attributes.Append(attr);
// move all the children of the row element into the new BPAddresses element
foreach (XmlElement child in row.GetElementsByTagName("*").Cast<XmlElement>().ToList())
{
addr.AppendChild(child);
}
// insert the new element after the last BPAddresses element
data2.InsertAfter(addr, insertionPoint);
insertionPoint = addr;
}
// remove the original BPAddresses element (and its children)
data2.RemoveChild(bpa);
var requestBody = JsonConvert.SerializeXmlNode(data2, Formatting.Indented, true);
Console.WriteLine(requestBody);
}
foreach(d1.GetElementsByTagName(“业务伙伴”)中的XmlElement数据2)
{
XmlElement bpa=(XmlElement)数据2.选择SingleNode(“BPAddresses”);
XmlElement insertionPoint=bpa;
foreach(bpa.GetElementsByTagName(“行”)中的XmlElement行)
{
//创建一个新的BPAddresses元素并向其添加Array=“true”属性
//因此JSON.Net将知道如何将其视为一个数组
XmlElement addr=d1.CreateElement(“BPAddresses”);
XmlAttribute attr=d1.CreateAttribute(“数组”http://james.newtonking.com/projects/json");
attr.Value=“true”;
addr.Attributes.Append(attr);
//将行元素的所有子元素移动到新的BPAddresses元素中
foreach(第行中的XmlElement子级.GetElementsByTagName(“*”).Cast().ToList())
{
地址:附属儿童(儿童);
}
//在最后一个BPAddresses元素之后插入新元素
数据2.InsertAfter(addr,insertionPoint);
insertionPoint=addr;
}
//删除原始BPAddresses元素(及其子元素)
数据2.RemoveChild(bpa);
var requestBody=JsonConvert.SerializeXmlNode(data2,Formatting.Indented,true);
Console.WriteLine(请求主体);
}
小提琴: