我想使用XmlDocument()來讀取節點的XML文件節點輸出每個元素。對具有xmlns屬性的節點使用XmlDocument()?
經過多次試錯後,我確定在我的節點上具有xmlns屬性不會導致從SelectNodes()調用返回節點。不知道爲什麼。
由於我無法更改輸出格式並且無法訪問實際的命名空間,因此我有什麼方法可以解決此問題?
另外,我有一些有子節點的元素。如何在循環瀏覽XML文件時訪問這些文件? I.E.,我需要解密CipherValue元素,但不知道如何訪問此節點?
示例如下:
doc.Load(@"test.xml");
XmlNodeList logEntryNodeList = doc.SelectNodes("/Logs/LogEntry");
Console.WriteLine("Nodes = {0}", logEntryNodeList.Count);
foreach (XmlNode xn in logEntryNodeList)
{
string dateTime = xn["DateTime"].InnerText;
string sequence = xn["Sequence"].InnerText;
string appId = xn["AppID"].InnerText;
Console.WriteLine("{0} {1} {2}", dateTime, sequence, appId);
}
示例XML看起來是這樣的:
<Logs>
<LogEntry Version="1.5" PackageVersion="10.10.0.10" xmlns="http://private.com">
<DateTime>2013-02-04T14:05:42.912349-06:00</DateTime>
<Sequence>5058</Sequence>
<AppID>TEST123</AppID>
<StatusDesc>
<EncryptedData>
<CipherData xmlns="http://www.w3.org/2001/04/xmlenc#">
<CipherValue>---ENCRYPTED DATA BASE64---</CipherValue>
</CipherData>
</EncryptedData>
</StatusDesc>
<Severity>Detail</Severity>
</LogEntry>
<LogEntry Version="1.5" PackageVersion="10.10.0.10" xmlns="http://private.com">
<DateTime>2013-02-04T14:05:42.912350-06:00</DateTime>
<Sequence>5059</Sequence>
<AppID>TEST123</AppID>
<StatusDesc>
<EncryptedData>
<CipherData xmlns="http://www.w3.org/2001/04/xmlenc#">
<CipherValue>---ENCRYPTED DATA BASE64---</CipherValue>
</CipherData>
</EncryptedData>
</StatusDesc>
<Severity>Detail</Severity>
</LogEntry>
</Logs>
我會試試看 - 如何處理子節點? – user500741
我改成.NET 4.0完整,但得到的。選擇 – user500741
錯誤此錯誤「System.Collections.Generic.IEnumerable」不包含「選擇」的定義和沒有擴展方法'Select'接受類型'System.Collections.Generic.IEnumerable '的第一個參數可以找到(你是否缺少using指令或程序集引用?) –
user500741