合并关键字段上的两个XML文件

时间:2017-03-31 15:55:34

标签: json xml vb.net

假设我有以下两个XML文件,其结构如下:

<supervisors>
     <supervisor id="1">
         <firstName>John</firstName>
         <lastName>Smith</lastName>
         <department>IT</department>
     </supervisor>
     <supervisor id="2">
         <firstName>Jack</firstName>
         <lastName>Jones</lastName>
         <department>HR</department>
     </supervisor>
     <supervisor id="3">
         <firstName>Allan</firstName>
         <lastName>Johnson</lastName>
         <department>Sales</department>
     </supervisor>
     <supervisor id="4">
         <firstName>Chris</firstName>
         <lastName>Reyes</lastName>
         <department>Shipping</department>
     </supervisor>
</supervisors>

<employees>   
     <employee supervisorID="1" employeeID="2">
         <firstName>Mike</firstName>
         <lastName>Albert</lastName>
         <phone>1111111111</phone>
     </employee>
     <employee supervisorID="1" employeeID="3">
         <firstName>Alicia</firstName>
         <lastName>James</lastName>
         <phone>1112222222</phone>
     </employee>
     <employee supervisorID="3" employeeID="4">
         <firstName>Anne</firstName>
         <lastName>Thomas</lastName>
         <phone>1112223333</phone>
     </employee>
</employees>

我想结束一个看起来像这个JSON的数据集(它可以是一个真实的数据集,但为此我展示了JSON,因为它更容易表示):

[
     {empFName: "Mike", empLName: "Albert", supFName: "John", supLName: "Smith", dept: "IT"}, 
     {empFName: "Alicia", empLName: "James", supFName: "John", supLName: "Smith", dept: "IT"}, 
     {empFName: "Anne", empLName: "Thomas", supFName: "Allan", supLName: "Johnson", dept: "Sales"}
 ]

如何组合XML数据以正确返回返回上述JSON的方式?

1 个答案:

答案 0 :(得分:3)

使用LINQ to XML使这变得简单,XML literals使它变得漂亮(如果你是这样的话) - LINQPad示例:

Dim supervisors = 
<supervisors>
     <supervisor id="1">
         <firstName>John</firstName>
         <lastName>Smith</lastName>
         <department>IT</department>
     </supervisor>
     <supervisor id="2">
         <firstName>Jack</firstName>
         <lastName>Jones</lastName>
         <department>HR</department>
     </supervisor>
     <supervisor id="3">
         <firstName>Allan</firstName>
         <lastName>Johnson</lastName>
         <department>Sales</department>
     </supervisor>
     <supervisor id="4">
         <firstName>Chris</firstName>
         <lastName>Reyes</lastName>
         <department>Shipping</department>
     </supervisor>
</supervisors>

Dim employees =
<employees>   
     <employee supervisorID="1" employeeID="2">
         <firstName>Mike</firstName>
         <lastName>Albert</lastName>
         <phone>1111111111</phone>
     </employee>
     <employee supervisorID="1" employeeID="3">
         <firstName>Alicia</firstName>
         <lastName>James</lastName>
         <phone>1112222222</phone>
     </employee>
     <employee supervisorID="3" employeeID="4">
         <firstName>Anne</firstName>
         <lastName>Thomas</lastName>
         <phone>1112223333</phone>
     </employee>
</employees>

Dim joined = (
    From e In employees.<employee>
    Join s In supervisors.<supervisor> On e.@supervisorID Equals s.@id
    Select New With {
        .empFName = e.<firstName>.Value,
        .empLName = e.<lastName>.Value,
        .supFName = s.<firstName>.Value,
        .supLName = s.<lastName>.Value,
        .dept = s.<department>.Value
    }
)

joined.Dump()

这导致:

LINQPad output

Json.NET可以将其转换为JSON。

Dim json = JsonConvert.SerializeObject(joined)
json.Dump()
[
    {"empFName":"Mike","empLName":"Albert","supFName":"John","supLName":"Smith","dept":"IT"},
    {"empFName":"Alicia","empLName":"James","supFName":"John","supLName":"Smith","dept":"IT"},
    {"empFName":"Anne","empLName":"Thomas","supFName":"Allan","supLName":"Johnson","dept":"Sales"}
]