我有一个问题。
我试图用Xmlreader类读取一个巨大的XML文件。 正常阅读和打印(控制台)工作正常。
但是现在我正在尝试过滤指定的子节点并且不起作用。
例如:
这是我的XML文件
`<?xml version="1.0" encoding="UTF-8"?>
<Event>
<System>
<Provider Name="NetApp-Security-Auditing" Guid="{3CB2A168-FE19-4A4E-BDAD-DCF422F13473}" />
<EventID>4660</EventID>
<EventName>Delete Object Attempt</EventName>
<Version>101.3</Version>
<Source>CIFS</Source>
<Level>0</Level>
<Opcode>0</Opcode>
<Keywords>0x8020000000000000</Keywords>
<Result>Audit Success</Result>
<TimeCreated SystemTime="2018-04-27T08:54:59.739617000Z" />
<Correlation />
<Channel>Security</Channel>
<Computer>ta-cluster01/svm_ta_cifs</Computer>
<ComputerUUID>7ec98fb6-de63-11e4-ba38-00a09864e708/0bbaf036-10e8-11e5-83fa-00a09864e708</ComputerUUID>
<Security />
</System>
<EventData>
<Data Name="SubjectIP" IPVersion="4">172.16.6.162</Data>
<Data Name="SubjectUnix" Uid="65534" Gid="65534" Local="false" />
<Data Name="SubjectUserSid">S-1-5-21-4259334589-2141180538-67931973-13448</Data>
<Data Name="SubjectUserIsLocal">false</Data>
<Data Name="SubjectDomainName">TDLZ2</Data>
<Data Name="SubjectUserName">hegelbachpatrik</Data>
<Data Name="ObjectServer">Security</Data>
<Data Name="ObjectType">File</Data>
<Data Name="HandleID">00000000000420;00;00011f73;13890cc2</Data>
<Data Name="ObjectName">(gruppe05_homes);/test/testuser/Vorlagen/LiveContent/16/Managed/Document Themes/1031/TM02836342[[fn=Ion]].thmx</Data>
<Data Name="InformationSet">Delete on last close;</Data>
</EventData>
</Event>`
现在我想检查子节点InformationSet的名称是否包含值&#34;删除最后一次关闭&#34;。如果这是True,它应该打印整个EventData节点
在您的下方,我会看到我尝试打印子节点的C#代码&#34;数据&#34; 但它没有用,任何人都可以帮助我吗?
var reader = XmlReader.Create(@"\\testserver\Example.xml");
var test = reader.ReadToDescendant("EventData");
while (reader.Read()&& reader.Name == "Data")
{
Console.WriteLine(reader.Value);
}
正确的文件看起来像这样
@jdweng
对不起,我应该说清楚。 XML文件看起来像这样。
<Events xmlns="http://www.netapp.com/schemas/ONTAP/2007/AuditLog">
<Event><System><Provider Name="NetApp-Security-Auditing" Guid="{3CB2A168-FE19-4A4E-BDAD-DCF422F13473}"/><EventID>4656</EventID><EventName>Open Object</EventName><Version>101.3</Version><Source>CIFS</Source><Level>0</Level><Opcode>0</Opcode><Keywords>0x8020000000000000</Keywords><Result>Audit Success</Result><TimeCreated SystemTime="2018-05-04T11:13:55.231818000Z"/><Correlation/><Channel>Security</Channel><Computer>ta/svm_ta_cifs</Computer><ComputerUUID>7ec98fb6-de63-11e4-ba38-00a09864e708/0bbaf036-10e8-11e5-83fa-00a09864e708</ComputerUUID><Security/></System><EventData><Data Name="SubjectIP" IPVersion="4">172.16.6.71</Data><Data Name="SubjectUnix" Uid="65534" Gid="65534" Local="false"></Data><Data Name="SubjectUserSid">S-1-5-21-4259334589-2141180538-67931973-2742</Data><Data Name="SubjectUserIsLocal">false</Data><Data Name="SubjectDomainName">test</Data><Data Name="SubjectUserName">test</Data><Data Name="ObjectServer">Security</Data><Data Name="ObjectType">File</Data><Data Name="HandleID">00000000000420;00;00015148;21fdd9f2</Data><Data Name="ObjectName">(gruppe05_homes);/tal/test/Scripte indiv/ISAGReports_RichTextWerbemitteilungen.txt</Data><Data Name="AccessList">%%4423 %%1537 </Data><Data Name="AccessMask">1080</Data><Data Name="DesiredAccess">Read Attributes; Delete; </Data><Data Name="Attributes">Open a non-directory; </Data></EventData></Event>
<Event><System><Provider Name="NetApp-Security-Auditing" Guid="{3CB2A168-FE19-4A4E-BDAD-DCF422F13473}"/><EventID>4660</EventID><EventName>Delete Object Attempt</EventName><Version>101.3</Version><Source>CIFS</Source><Level>0</Level><Opcode>0</Opcode><Keywords>0x8020000000000000</Keywords><Result>Audit Success</Result><TimeCreated SystemTime="2018-05-04T11:13:55.238819000Z"/><Correlation/><Channel>Security</Channel><Computer>ta/svm_ta_cifs</Computer><ComputerUUID>7ec98fb6-de63-11e4-ba38-00a09864e708/0bbaf036-10e8-11e5-83fa-00a09864e708</ComputerUUID><Security/></System><EventData><Data Name="SubjectIP" IPVersion="4">172.16.6.71</Data><Data Name="SubjectUnix" Uid="65534" Gid="65534" Local="false"></Data><Data Name="SubjectUserSid">S-1-5-21-4259334589-2141180538-67931973-2742</Data><Data Name="SubjectUserIsLocal">false</Data><Data Name="SubjectDomainName">test</Data><Data Name="SubjectUserName">test</Data><Data Name="ObjectServer">Security</Data><Data Name="ObjectType">File</Data><Data Name="HandleID">00000000000420;00;00015148;21fdd9f2</Data><Data Name="ObjectName">(gruppe05_homes);/tal/test/ISAGReports_RichTextWerbemitteilungen.txt</Data><Data Name="InformationSet">Delete on last close; </Data></EventData></Event>
</Events>
答案 0 :(得分:1)
请尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
var reader = XmlReader.Create(FILENAME);
reader.MoveToContent();
string ns = reader.NamespaceURI;
//if this doesn't work hard code ns
// string ns = "http://www.netapp.com/schemas/ONTAP/2007/AuditLog";
while (!reader.EOF)
{
if (reader.Name != "EventData")
{
reader.ReadToFollowing("EventData",ns);
}
if (!reader.EOF)
{
XElement eventData = (XElement)XElement.ReadFrom(reader);
foreach (XElement data in eventData.Elements("Data"))
{
Console.WriteLine("{0} : {1}", (string)data.Attribute("Name"), (string)data);
}
}
}
Console.ReadLine();
}
}
}
答案 1 :(得分:1)
using (var reader = XmlReader.Create("test.xml"))
{
XNamespace ns = "http://www.netapp.com/schemas/ONTAP/2007/AuditLog";
while (reader.ReadToFollowing("EventData"))
{
var eventData = (XElement)XElement.ReadFrom(reader);
var data = eventData.Elements(ns + "Data")
.FirstOrDefault(e => e.Attribute("Name").Value == "InformationSet");
if (data != null && data.Value.Contains("Delete on last close"))
{
// use eventData somehow
Console.WriteLine(eventData);
}
}
}
答案 2 :(得分:0)
您可以在C#中读取节点/子节点,如下所示:
XmlDocument source = new XmlDocument();
//local or http file
source.Load("C:\\Users\\path\\myFile.xml");
foreach (XmlNode node in source.DocumentElement.SelectNodes("/parentNode/xmlTag/targetNode"))
{
string text = node.InnerText;
//do your stuff with XML value here
System.Diagnostics.Debug.WriteLine(text);
}
方法SelectNodes()应该这样做。