1
<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 个“活动”元素,内容级别各不相同。有些具有“客户”和“物质”元素,有些则没有。把它想象成一个表格,这些将是空白单元格,但列标题仍然存在。

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

 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();

        }

到目前为止的代码。输出如下所示

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

我基本上需要提取每条信息,这样我就可以将它们存储在一个表格布局中。我对使用 XML 解析相当陌生,因此将不胜感激。

4

4 回答 4

0

只有您知道允许的属性名称cabinet...Client。简单的蛮力方法是提取每个预期的属性,然后您将知道哪些属性丢失并且可以将单元格设置为空。Foreach 只会迭代每个元素上存在的内容 - 它无法猜测缺失的内容。

于 2013-08-06T14:37:12.267 回答
0

尝试类似的东西。创建一个新的Windows Forms Application,将一个DataGrid控件添加到表单和后面的代码中,如下所示:

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();

        }

希望这可以帮助。

于 2013-08-08T08:46:02.683 回答
0

I think you could solve your problem in the following way:

  1. You create a class called BaseNode.

  2. You create classes which extend BaseNode for all of your entity types

  3. You create a set of rules which based on the node determine the preferred entity type

  4. You create a generateEntity method in your BaseNode class.

  5. You use this algorithm (this is not code, so do not try to compile it)

parseXML(node)

for each node in node do

    BaseNode.generateEntity(node.input)

    if (node.hasChildren())

        parseXML(node)

    end if

end for

end parseXML

Of course, you have to store and parse the generated entities.

于 2013-08-06T14:54:13.153 回答
0

并不是说这是解决您的特定问题的最佳或正确方法,但是,我将其作为您可以做什么的简略示例提供(因此缺少异常/错误处理等)。

namespace so.consoleapp
{
    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            var doc = XElement.Load("file.xml");
            var activityElements = doc.Elements("activity");

            ICollection<Activity> collectionOfActivities = new List<Activity>();
            foreach (var activityElement in activityElements)
            {
                var storageObjectElement = activityElement.Element("storageObject");

                string clientElement = null;
                if (storageObjectElement.Element("Client") != null)
                {
                    clientElement = storageObjectElement.Element("Client").Value;
                }

                var newStorageObject = new StorageObject
                {
                    Client = clientElement,
                    Author = storageObjectElement.Element("Author").Value
                };

                var userElement = activityElement.Element("user");
                var newUser = new User
                {
                    Id = userElement.Attribute("id").Value,
                    Name = userElement.Attribute("name").Value,
                    MemberType = userElement.Attribute("memberType").Value
                };

                collectionOfActivities.Add
                (
                    new Activity
                    {
                        Date = activityElement.Attribute("date").Value,
                        Name = activityElement.Attribute("name").Value,
                        Host = activityElement.Attribute("host").Value,
                        User = newUser,
                        StorageObject = newStorageObject
                    }
                );
            }

            Console.ReadLine();
        }
    }

    class Activity
    {
        public string Date
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Host
        {
            get;
            set;
        }

        public User User
        {
            get;
            set;
        }

        public StorageObject StorageObject
        {
            get;
            set;
        }
    }

    class User
    {
        public string Id
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string MemberType
        {
            get;
            set;
        }
    }

    class StorageObject
    {
        public string Client
        {
            get;
            set;
        }

        public string Author
        {
            get;
            set;
        }
    }
}
于 2013-08-06T15:07:17.533 回答