Reputation: 292
I have below 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>
I want to display totalAvailable, view name, view id, view content url, totatviewcount in my angular 2 application.
I have converted my xml to 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"
}
}
]
}
}
]
}
}
]
}
}}
but its becoming more complex because every time xml will have different child nodes. Please let me know if you have an idea about this scenario. Thanks in advance.
Upvotes: 1
Views: 86
Reputation: 6332
May be you want to try with Cinchoo ETL - an open source file helper to work with different file formats.
Here is how you can accomplish you needs
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);
}
}
}
Output:
[
{
"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
}
]
Hope this helps.
Upvotes: 2