<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="[email protected]" 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個「活動」元素,內容不同。有些人擁有「客戶」和「重要」元素,其他人則沒有。把它想象成一個表格,這些將是空白單元格,但列標題仍然存在。C#中的複雜嵌套XML解析#
我基本上需要解析這個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解析合理新,所以任何幫助,將不勝感激。
是否有一個特殊的原因,您想手動解析它?爲什麼不創建相應的c#類並將其反序列化爲這些類? – derape
爲什麼你不序列化它http://msdn.microsoft.com/en-us/library/58a18dwa.aspx – Krishna
@derape我是在假設,我必須做'XDocument'或LINQ –