【问题标题】:How to read all nodes value with nodes name from XmlDocument如何从 XmlDocument 中读取具有节点名称的所有节点值
【发布时间】:2017-12-08 14:05:10
【问题描述】:

我有以下 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、视图内容 url、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 都会有不同的子节点。 如果您对这种情况有任何想法,请告诉我。 提前致谢。

【问题讨论】:

  • 生成的 json 将始终具有相同的结构。或者您的 xml 本身是动态的。在这里感到困惑。
  • @gusaindpk,xml也是动态的,有些参数返回不同的子节点。

标签: c# json xml angular


【解决方案1】:

您可能想尝试使用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
 }
]

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多