[英]Deserialize XML string to Object Error : There is an Error in xml document (1,2)
From windows event viewer I can get the following xml structure: 从Windows事件查看器我可以得到以下xml结构:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="XXXXXXXXXX" Guid="{YYYYYYYY}" />
<EventID>XYZ</EventID>
<Version>0</Version>
<Level>L</Level>
<Task>A</Task>
<Opcode>0</Opcode>
<Keywords>0x000xyzh</Keywords>
<TimeCreated SystemTime="2012-06-28T15:44:04.997837000Z" />
<EventRecordID>153</EventRecordID>
<Correlation ActivityID="{DDDDDDDDD}" />
<Execution ProcessID="199999" ThreadID="90990" />
<Channel>Microsoft-Windows-ABCDEFG/Admin</Channel>
<Computer>myPC</Computer>
<Security UserID="ABCABC" />
</System>
<EventData>
<Data Name="name1">data1</Data>
<Data Name="name2">data2</Data>
<Data Name="name3">data3</Data>
</EventData>
<RenderingInfo Culture="en-US">
<Message>some message </Message>
<Level>Information</Level>
<Task>XYZ</Task>
<Opcode>Info</Opcode>
<Channel />
<Provider />
<Keywords>
<Keyword>XYZ</Keyword>
</Keywords>
</RenderingInfo>
</Event>
I am only interested in the EventData section of the xml. 我只对xml的EventData部分感兴趣。 I have created the following very simple classes:
我创建了以下非常简单的类:
public class Event
{
public EventData EventData;
}
public class EventData
{
public String[] Data;
}
I then use the following code: 然后我使用以下代码:
XmlSerializer serializer = new XmlSerializer(typeof(Event));
StringReader reader = new StringReader(evtXml);
evt = (Event)serializer.Deserialize(reader);
but on the first line of code, I get the following error: 但在第一行代码中,我收到以下错误:
There is an error in XML document (1, 2).
This error is not informative to me. 这个错误对我没有任何帮助。 Is the problem that I don't have all the fields in the classes or do I need some other class (other than XmlSerializer) to get the data from.
问题是我没有类中的所有字段,或者我是否需要其他类(XmlSerializer除外)来从中获取数据。 The way I would like the data under the EventData is by name and data value (eg name1 with data1) ...etc
我希望EventData下的数据的方式是名称和数据值(例如带有data1的name1)......等等
Important EDIT : the xml I am getting is generated by the ToXML() method of the EventRecord class 重要编辑 :我得到的xml是由EventRecord类的ToXML()方法生成的
Thanks 谢谢
XmlSerializer serializer = new XmlSerializer(typeof(Event),
"http://schemas.microsoft.com/win/2004/08/events/event");
StringReader reader = new StringReader(evtXml);
var evt = (Event)serializer.Deserialize(reader);
public class Event
{
public Data[] EventData;
}
public class Data
{
[XmlAttribute]
public string Name;
[XmlText]
public string Value;
}
XmlSerializer often tells you what the matte is; XmlSerializer经常告诉你遮罩是什么; add some error handling, specifically:
添加一些错误处理,具体来说:
try {
// your code
} catch(Exception ex) {
while(ex != null) {
Console.WriteLine(ex.Message);
ex = ex.InnerException;
}
}
I'm guessing it is a namespace issue; 我猜它是命名空间问题; try:
尝试:
[XmlRoot("Event",
Namespace="http://schemas.microsoft.com/win/2004/08/events/event")]
public class Event {...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.