C# 将 XML 字符串反序列化为对象错误:xml 文档中存在错误 (1,2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11250992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:08:02  来源:igfitidea点击:

Deserialize XML string to Object Error : There is an Error in xml document (1,2)

c#.netwindowsxml-serializationxmlserializer

提问by Saher Ahwal

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. I have created the following very simple classes:

我只对 xml 的 EventData 部分感兴趣。我创建了以下非常简单的类:

   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).

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. The way I would like the data under the EventData is by name and data value (e.g name1 with data1) ...etc

这个错误对我来说没有信息。问题是我没有类中的所有字段,还是我需要一些其他类(XmlSerializer 除外)来获取数据。我希望 EventData 下的数据的方式是按名称和数据值(例如 name1 和 data1)...等

Important EDIT: the xml I am getting is generated by the ToXML()method of the EventRecordclass

重要编辑:我得到的 xml 是由EventRecord类的ToXML()方法生成的

Thanks

谢谢

采纳答案by Markus Jarderot

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;
}

回答by Marc Gravell

XmlSerializer often tells you what the matte is; add some error handling, specifically:

XmlSerializer 经常告诉你遮罩是什么;添加一些错误处理,特别是:

try {
   // your code
} catch(Exception ex) {
    while(ex != null) {
        Console.WriteLine(ex.Message);
        ex = ex.InnerException;
    }
}

I'm guessingit is a namespace issue; try:

这是一个命名空间问题;尝试:

[XmlRoot("Event",
    Namespace="http://schemas.microsoft.com/win/2004/08/events/event")]
public class Event {...}