C# XML 文档中存在错误 (1, 41)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9821682/
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
There is an error in XML document (1, 41)
提问by Pradeep
When i am doing Deserialize of xml i am getting "There is an error in XML document (1, 41)." . Can anyone tell me about what is the issue is all about.
当我对 xml 进行反序列化时,我收到“XML 文档中存在错误 (1, 41)”。. 任何人都可以告诉我问题是什么。
public static T DeserializeFromXml<T>(string xml)
{
T result;
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}
I use this function to do it.
我使用这个功能来做到这一点。
<?xml version='1.0' encoding='utf-16'?>
<Message>
<FirstName>Hunt</FirstName>
<LastName>DAvid</LastName>
</Message>
采纳答案by sll
Ensure your Message class looks like below:
确保您的 Message 类如下所示:
[Serializable, XmlRoot("Message")]
public class Message
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This works for me fine:
这对我来说很好:
string xml = File.ReadAllText("c:\Message.xml");
var result = DeserializeFromXml<Message>(xml);
The name of the XML root element that is generated and recognized in an XML-document instance. The default is the name of the serialized class.
在 XML 文档实例中生成和识别的 XML 根元素的名称。默认值为序列化类的名称。
So it might be your class name is not Messageand this is why deserializer was not able find it using default behaviour.
所以它可能是你的类名不是Message,这就是为什么反序列化器无法使用默认行为找到它。
回答by Neville
Agreed with the answer from sll, but experienced another hurdle which was having specified a namespace in the attributes, when receiving the return xml that namespace wasn't included and thus failed finding the class.
同意 sll 的回答,但遇到了另一个障碍,即在属性中指定了命名空间,当收到返回的 xml 时,该命名空间未包含在内,因此无法找到该类。
i had to find a workaround to specifying the namespace in the attribute and it worked.
我必须找到一种解决方法来指定属性中的命名空间并且它起作用了。
ie.
IE。
[Serializable()]
[XmlRoot("Patient", Namespace = "http://www.xxxx.org/TargetNamespace")]
public class Patient
generated
生成
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxxx.org/TargetNamespace">
but I had to change it to
但我不得不把它改成
[Serializable()]
[XmlRoot("Patient")]
public class Patient
which generated to
这产生了
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
This solved my problem, hope it helps someone else.
这解决了我的问题,希望它可以帮助别人。
回答by Dan
I had the same thing. All came down to a "d" instead of a "D" in a tag name in the schema.
我有同样的事情。所有都归结为模式中标签名称中的“d”而不是“D”。
回答by Gopal Pendalwar
First check the variables declared using proper Datatypes. I had a same problem then I have checked, by mistake I declared SAPUser as int datatype so that the error occurred. One more thing XML file stores its data using concept like array but its first index starts having +1. e.g. if error is in(7,2) then check for 6th line always.....
首先检查使用正确数据类型声明的变量。我遇到了同样的问题,然后我进行了检查,错误地将 SAPUser 声明为 int 数据类型,因此发生了错误。另一件事 XML 文件使用类似数组的概念存储其数据,但它的第一个索引开始具有 +1。例如,如果错误在 (7,2) 中,则始终检查第 6 行.....
回答by user547176
On a WEC7 project I'm working on, I got a similar error. The file I was serializing in was serialized out from an array of objects, so I figured the XML was fine. Also, I have had this working for a few previous classes, so it was quite a puzzle.
在我正在进行的 WEC7 项目中,我遇到了类似的错误。我正在序列化的文件是从对象数组中序列化出来的,所以我认为 XML 没问题。此外,我在之前的一些课程中已经使用过这个,所以这是一个相当大的难题。
Then I noticed in my earlier work that every class that I was serializing/deserializing had a default constructor. That was missing in my failed case so I added it and and voila... it worked fine.
然后我注意到在我早期的工作中,我正在序列化/反序列化的每个类都有一个默认构造函数。在我失败的案例中缺少这一点,所以我添加了它,瞧……效果很好。
I seem to remember reading somewhere that this was required. I guess it is.
我似乎记得在某个地方读到过这是必需的。我想是的。
回答by Sofia Khwaja
In my case I had a float value expected where xml had a null value so be sure to search for float and int data type in your xsd map
在我的情况下,我有一个预期的浮点值,其中 xml 有一个空值,所以一定要在 xsd 映射中搜索 float 和 int 数据类型

