我的xml以下: -
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="abc.com/api" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="abc.com/api abc.com/api/ts-api-2.3.xsd">
<pagination pageNumber="1" pageSize="100" totalAvailable="2" />
<views>
<view id="2adaf1b2" name=" Users by Function" contentUrl="ExampleWorkbook/sheets/UsersbyFunction">
<workbook id="9fb2948d" />
<owner id="c2abaaa9" />
<usage totalViewCount="5388" />
</view>
<view id="09ecb39a" name=" Users by Site" contentUrl="ExampleWorkbook/sheets/UsersbySite">
<workbook id="9fb2948d" />
<owner id="c2abaaa9" />
<usage totalViewCount="95" />
</view>
</views>
</tsResponse>
我想在我的angular 2应用程序中显示totalAvailable,视图名称,视图ID,查看内容网址,totatviewcount。
我已将我的xml转换为json: -
{{
"tsResponse": {
"xmlns": "abc.com/api",
"xsi": "www.w3.org/2001/XMLSchema-instance",
"schemaLocation": "abc.com/api abc.com/api/ts-api-2.3.xsd",
"_value": [
{
"pagination": {
"pageNumber": "1",
"pageSize": "1000",
"totalAvailable": "2"
}
},
{
"views": {
"_value": [
{
"view": {
"id": "2adaf1b2",
"name": " Users by Function",
"contentUrl": "ExampleWorkbook/sheets/UsersbyFunction",
"_value": [
{
"workbook": {
"id": "9fb2948d"
}
},
{
"owner": {
"id": "c2abaaa9"
}
},
{
"usage": {
"totalViewCount": "57"
}
}
]
}
},
{
"view": {
"id": "09ecb39a",
"name": " Users by Site",
"contentUrl": "ExampleWorkbook/sheets/UsersbySite",
"_value": [
{
"workbook": {
"id": "9fb2948d"
}
},
{
"owner": {
"id": "c2abaaa9"
}
},
{
"usage": {
"totalViewCount": "9"
}
}
]
}
}
]
}
}
]
}
}}
但它变得越来越复杂,因为每次xml都有不同的子节点。 如果您对此方案有所了解,请告诉我。 提前谢谢。
答案 0 :(得分:2)
可能您想尝试使用Cinchoo ETL - 一个开源文件助手来处理不同的文件格式。
以下是如何满足您的需求
static void Test()
{
int totalAvailable;
using (var parser = new ChoXmlReader("sample9.xml", "abc.com/api").WithXPath("/tsResponse/pagination")
.WithField("totalAvailable", fieldType: typeof(int))
)
{
totalAvailable = parser.FirstOrDefault().totalAvailable;
}
using (var parser = new ChoXmlReader("sample9.xml", "abc.com/api").WithXPath("/tsResponse/views/view")
.WithField("view_id", xPath: "@id")
.WithField("view_name", xPath: "@name")
.WithField("view_content_url", xPath: "@contentUrl")
.WithField("view_total_count", xPath: "/x:usage/@totalViewCount", fieldType: typeof(int))
)
{
using (var writer = new ChoJSONWriter("sample9.json")
)
{
foreach (dynamic rec in parser)
writer.Write(new { view_id = rec.view_id, view_name = rec.view_name, view_content_url = rec.view_content_url, view_total_count = rec.view_total_count, view_total_available = totalAvailable });
writer.Write(parser);
}
}
}
输出:
[
{
"view_id":"2adaf1b2",
"view_name":"Users by Function",
"view_content_url":"ExampleWorkbook/sheets/UsersbyFunction",
"view_total_count":95,
"view_total_available":2
},
{
"view_id":"09ecb39a",
"view_name":"Users by Site",
"view_content_url":"ExampleWorkbook/sheets/UsersbySite",
"view_total_count":95,
"view_total_available":2
}
]
希望这有帮助。