简体   繁体   English

查询存储在属性中的XML值并使用C#导入到数据集中

[英]Query XML values stored in attributes & import into dataset using C#

I am new to c# in general and new to parsing xml. 我是c#的新手,也是解析xml的新手。 I have an xml stream that I am getting from a web service that has values in attributes that I want to load into my SQL Server using a dataset/datatable. 我有一个xml流,我从一个Web服务获得,该服务具有我想使用数据集/数据表加载到我的SQL Server的属性值。 I am having a hard time getting the data in the structure that I want to import into SQL. 我很难在结构中获取要导入SQL的数据。

I would like to pull the data (for each into a tables in the following structure: 我想将数据(每个数据拉入以下结构的表中:

Columns: bounce_date cancellation_mailing_instance_id cancellation_message cancellation_date email, etc. 列:bounce_date cancellation_mailing_instance_id cancellation_message cancellation_date电子邮件等

Rows: bounce_date "value" cancellation_mailing_instance_id "value" cancellation_message "value" cancellation_date "value" email "value", etc. 行:bounce_date“value”cancellation_mailing_instance_id“value”cancellation_message“value”cancellation_date“value”email“value”等。

So far all I can accomplish is getting all the data into rows. 到目前为止,我所能做的就是将所有数据放入行中。 I would like to create the column headers based on , etc 我想基于等创建列标题

...and the Rows based on "Value" "Value", etc. ......以及基于“价值”“价值”等的行

My XML is coming over as follows: 我的XML即将发布如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<GridResponse xmlns="http://sample.sample.net/aapi/2009/08/">
<Brand id="12345">ACME</Brand>
<User>asample</User>
<Grids>
<Grid type="subscriber">
<Record row="1">
<Fields>
<Field element="bounce_date"/>
<Field element="cancellation_mailing_instance_id"/>
<Field element="cancellation_message"/>
<Field element="cancellation_date">2018-03-21T16:00:25.7670000Z</Field>
<Field element="email"/>
<Field element="is_repeated_bouncer">0</Field>
<Field element="is_unsubscriber">1</Field>
<Field element="modified_date">2018-03-21T16:00:29.6500000Z</Field>
<Field element="service_since_date">2018-03-21T13:45:50.2800000Z</Field>
<Field element="user_id"/>
<Field element="subscriber_upload_id"/>
<Field element="imis_name_id"/>
</Fields>
</Record>
<Record row="2">
<Fields>
<Field element="bounce_date"/>
<Field element="cancellation_mailing_instance_id"/>
<Field element="cancellation_message"/>
<Field element="cancellation_date"/>
<Field element="email">xxxxx@sample.org</Field>
<Field element="is_repeated_bouncer">0</Field>
<Field element="is_unsubscriber">0</Field>
<Field element="modified_date">2018-03-21T14:07:32.1530000Z</Field>
<Field element="service_since_date">2018-03-21T14:07:32.1530000Z</Field>
<Field element="user_id"/>
<Field element="subscriber_upload_id"/>
<Field element="imis_name_id"/>
</Fields>
</Record>
<Record row="3">
<Fields>
<Field element="bounce_date"/>
<Field element="cancellation_mailing_instance_id"/>
<Field element="cancellation_message"/>
<Field element="cancellation_date"/>
<Field element="email">xxxxx2@sample.org</Field>
<Field element="is_repeated_bouncer">0</Field>
<Field element="is_unsubscriber">0</Field>
<Field element="modified_date">2019-04-22T20:03:33.9700000Z</Field>
<Field element="service_since_date">2019-04-22T20:03:33.9700000Z</Field>
<Field element="user_id"/>
<Field element="subscriber_upload_id"/>
<Field element="imis_name_id"/>
</Fields>
</Record>
</Grid>
</Grids>
</GridResponse>

So far I am trying Linq To XML. 到目前为止,我正在尝试使用Linq To XML。 I would be eternally grateful if someone could provide a little code sample to get me started! 如果有人能提供一些代码示例来帮我入门,我将永远感激不尽! Thanks in advance. 提前致谢。

He is some real simple Xml Linq that reads results into a dictionary 他是一个真正简单的Xml Linq,它将结果读入字典

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication110
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            Dictionary<int, List<string>> results = doc.Descendants(ns + "Record")
                .GroupBy(x => (int)x.Attribute("row"), y => y)
                .ToDictionary(x => x.Key, y => y.Descendants(ns + "Field").Select(z => (string)z.Attribute("element")).ToList());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM