C#中的复杂嵌套XML解析 [英] Complex nested XML Parsing in C#

查看:116
本文介绍了C#中的复杂嵌套XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<ndActivityLog repositoryId="AA-AAAA1AAA" repositoryName="Company Name" startDate="2013-07-05" endDate="2013-07-06">
    <activity date="2013-07-05T06:42:35" name="open" host="00.00.00.00">
        <user id="joebloggs@email.com" name="Joe Bloggs" memberType="I" /> 
        <storageObject docId="0000-0000-0000" name="Opinion" size="356864" fileExtension="doc">
            <cabinet name="Client and Matters">NG-5MIYABBV</cabinet> 
            <DocumentType>Legal Document</DocumentType> 
            <Author>Joe Bloggs</Author> 
            <Matter>1001</Matter> 
            <Client>R1234</Client> 
        </storageObject>
    </activity>
</ndActivityLog>

这是XML的示例.文档中大约有4000个活动"元素,内容的级别各不相同.有些包含客户"和事项"元素,有些则没有.可以将其视为表格,它们将是空白单元格,但列标题仍然存在.

This is an example of the XML. There's around 4000 "activity" elements within the document, with varying levels of content. Some have the "Client" and "Matter" elements, others don't. To think of it like a table, these would be blank cells, but the column headers are still there.

我基本上需要将此解析为SQL数据库,并保持数据结构.最重要的是,如果某些示例中不存在某个元素,则需要引用该事实并将其保留为空白单元格".

I essentially need to parse this into an SQL database, keeping the data structure. On top of this, if an element doesn't exist in certain examples, it needs to reference that fact and leave it as a "blank cell".

 var doc = XDocument.Load(path + "\\" + file + ".xml");

        var root = doc.Root;
        foreach (XElement el in root.Elements())
        {

               // Console.WriteLine(el.Nodes());
                //  Console.WriteLine(el.Value);
                //Console.WriteLine("  Attributes:");
                foreach (XAttribute attr in el.Attributes())
                {

                    Console.WriteLine(attr);
                 //   Console.WriteLine(el.Elements("id"));


                }

           Console.WriteLine("---------------------------");

          // foreach (XElement element in el.Elements())
       //    {

     //          Console.WriteLine("    {0}: {1}", element.Name, element.Value);
      //     }

           }
            //hold console open
            Console.ReadLine();

        }

到目前为止,

代码.输出如下所示

Code thus far. The output is shown below

date="2013-07-06T17:07:42"
name="open"
host="213.146.142.50

我基本上需要提取每条信息,因此我可以将它们基本上存储在表格布局中. 我对于使用XML解析是相当陌生的,因此可以提供任何帮助.

I basically need every piece of information to be extracted so I can store them in essentially a table layout. I'm reasonably new to using XML parsing, so any help would be appreciated.

推荐答案

尝试类似的方法.创建一个新的Windows Forms Application,向窗体中添加一个DataGrid控件,并在后面添加代码,如下所示:

Try something like that. Create a new Windows Forms Application, add one DataGrid control to the form and code behind like below:

private void Form1_Load(object sender, EventArgs e)
        {
            populate_datagrid(dataGridView1);
        }

        private void populate_datagrid(DataGridView dataGridView1)
        {
            String xml_string = @"<ndActivityLog repositoryId=""AA-AAAA1AAA"" repositoryName=""Company Name"" startDate=""2013-07-05"" endDate=""2013-07-06"">
                                    <activity date=""2013-07-05T06:42:35"" name=""open"" host=""00.00.00.00"">
                                        <user id=""joebloggs@email.com"" name=""Joe Bloggs"" memberType=""I"" /> 
                                        <storageObject docId=""0000-0000-0000"" name=""Opinion"" size=""356864"" fileExtension=""doc"">
                                            <cabinet name=""Client and Matters"">NG-5MIYABBV</cabinet> 
                                            <DocumentType>Legal Document</DocumentType> 
                                            <Author>Joe Bloggs</Author> 
                                            <Matter>1001</Matter> 
                                            <Client>R1234</Client> 
                                        </storageObject>
                                    </activity>
                                    <activity date=""2013-06-05T06:42:35"" name=""close"" host=""00.00.00.00"">
                                        <user id=""abc@bca.com"" name=""abc"" memberType=""I"" /> 
                                        <storageObject docId=""0000-0000-0000"" name=""Opinion"" size=""25630"" fileExtension=""doc"">
                                            <cabinet name=""Client and Matters"">NG-5MIYABBV</cabinet> 
                                            <DocumentType>Legal Document</DocumentType> 
                                            <Author>abc</Author> 
                                            <Client>R1234</Client> 
                                        </storageObject>
                                    </activity>
                                    <activity date=""2013-06-05T06:42:35"" name=""unknown"" host=""00.00.00.00"">
                                        <user id=""bca@abc.com"" name=""bca"" memberType=""I"" /> 
                                        <storageObject docId=""0000-0000-0000"" name=""Opinion"" size=""45875"" fileExtension=""doc"">
                                            <cabinet name=""Client and Matters"">NG-5MIYABBV</cabinet> 
                                            <DocumentType>Legal Document</DocumentType> 
                                            <Author>bca</Author> 
                                            <Matter>1001</Matter> 
                                        </storageObject>
                                    </activity>
                                    <activity date=""2013-06-05T06:42:35"" name=""open"" host=""00.00.00.00"">
                                        <user id=""cab@abc.com"" name=""cab"" memberType=""I"" /> 
                                        <storageObject docId=""0000-0000-0000"" name=""Opinion"" size=""45875"" fileExtension=""doc"">
                                            <cabinet name=""Client and Matters"">NG-5MIYABBV</cabinet> 
                                            <DocumentType>Legal Document</DocumentType>
                                        </storageObject>
                                    </activity>
                                </ndActivityLog>";

            var query = from XElement c in System.Xml.Linq.XElement.Parse(xml_string).Descendants("activity")
                        select new
                        {
                            user = c.Elements("user").First().Attribute("name").Value,
                            author = c.Descendants("Author").Count() > 0 ? c.Descendants("Author").First().Value : "n/a",
                            matter = c.Descendants("Matter").Count() > 0 ? c.Elements("Matter").First().Value : "n/a"
                        };

            dataGridView1.DataSource = query.ToList();

        }

希望这会有所帮助.

这篇关于C#中的复杂嵌套XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆