我将开始阅读所有这个问题的其他答案,并且所有这些答案(尽管是好的解决方案)在我的情况下都不起作用将XML反序列化为c#类时从xsd.exe派生的命名空间导致的错误
我创建一个C#类从我的XSD文件与
XSD.EXE/C neworder.xsd
它产生的一类7000+线,所以我会后它的相关部分。
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Generic;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.foo.com/schemas/w/v1.0")]
[System.Xml.Serialization.XmlRootAttribute("new-order", Namespace = "http://www.foo.com/schemas/w/v1.0")]
public partial class neworder
{
private List<customertype> customersField;
private string suppliercodeField;
private string versionField;
private static System.Xml.Serialization.XmlSerializer serializer;
public neworder()
{
this.customersField = new List<customertype>();
}
[System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
[System.Xml.Serialization.XmlArrayItemAttribute("customer",IsNullable = false)]
public List<customertype> customers
{
get
{
return this.customersField;
}
set
{
this.customersField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("supplier-code")]
public string suppliercode
{
get
{
return this.suppliercodeField;
}
set
{
this.suppliercodeField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("version")]
public string version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(neworder));
}
return serializer;
}
}
public static neworder Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((neworder)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
这只是一个小片段,其余的现在,因为我觉得如果这被解决了,我可以解决其余的并不重要。
这是XML文件的开头部分
<new-order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.foo.com/schema/w/v1.0" version="1.0" supplier-code="FAKECODE" schemaLocation="http://www.foo.com/schemas/w/v1.0/TransmissionEnvelope.xsd">
<customers>
<customer w-number="123456" customer-number="12345-12345">
<client-info>
<client-name>John Doe</client-name>
<w-id>433348</w-id>
</client-info>
<transferee-name>
<first>John</first>
<last>Doe</last>
</transferee-name>
<spouse-name>
<first />
<last />
</spouse-name>
<o-address>
<street1>123 Fake st</street1>
<city>Fakeville</city>
<state>CA</state>
<postal-code>90210</postal-code>
<country>USA</country>
</o-address>
<d-address>
<street1 />
<city>harbour</city>
<state>VA</state>
<postal-code>55555</postal-code>
<country>USA</country>
</d-address>
<contact-info>
<phone>
<phone-type>CELL</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>HOME</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>WORK</phone-type>
<phone-number />
</phone>
<email>[email protected]</email>
<comments>Just any comments here</comments>
</contact-info>
</customer>
</customers>
</new-order>
我尝试,我得到以下
XmlSerializer ser = new XmlSerializer(typeof(neworder));
neworder feed = (neworder)ser.Deserialize(new FileStream(filePath, FileMode.Open)) ;
错误反序列化是臭名昭著的:
有XML文档中出现错误(1,2)。 http://www.foo.com/schema/w/v1.0'>没有预料到。
我已经一遍又一遍地读过关于确保根音符属性为XMLROOT的问题。它有正确的命名空间。
我试着将XmlRootAttribute更改为XmlRoot。没有。 我试过删除名称空间并重新执行该类,但没有任何内容。
破坏我的大脑,因为一切似乎都合法。
发布架构和XML文件。 – Aybe
你可以发布一个完整的XML文件(也许是一个剥离)?我可以通过向'schema'添加's'来阻止异常:'xmlns =“http://www.foo.com/schemas/w/v1.0”',但我不知道如果通过仅使用“”元素的“xml文件”进行测试存在问题。 –
Quantic
添加了XML文件。不希望发布模式,因为它不是** my **模式。 我做了临时解决方法(我们将看看它是否适用于所有测试文档)是我使用xsd.exe从其中一个测试文档创建了一个XSD文件,然后我从中创建了该类。它在一个XML文档上工作,但我尝试了多个。 不是我想去的方式,但是,哦,好。 – reindeerKurt