简体   繁体   English

C#将XML反序列化为对象:XML文档中存在错误(3,2)

[英]C# Deserialize XML to object: There is an error in XML document (3, 2)

I'm trying to deserialize XML into a C# object 我正在尝试将XML反序列化为C#对象

I am getting the error:There is an error in XML document (3, 2). 我收到错误:XML文档中存在错误(3,2)。

Cannot seem to fix it! 似乎无法解决它! Here is the code: 这是代码:

The XSD is: XSD是:

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2012 Developer Edition (Trial) 10.0.2.3955     (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://www.adamroe.com/xsd/cameras.xsd" elementFormDefault="qualified" targetNamespace="http://www.adamroe.com/xsd/cameras.xsd"     xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="CameraBase">
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element name="Cameras">
                <xs:complexType>
                    <xs:sequence minOccurs="0" maxOccurs="unbounded">
                        <xs:element name="Camera" type="tns:CameraType" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="CameraType">
    <xs:sequence>
        <xs:element name="Make" type="xs:string" />
        <xs:element name="Model" type="xs:string" />
        <xs:element name="Variable1" type="xs:double" />
        <xs:element name="Variable2" type="xs:double" />
    </xs:sequence>
</xs:complexType>
</xs:schema>

The XML is: XML是:

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2012 Developer Edition (Trial) 10.0.2.3955     (http://www.liquid-technologies.com) -->
<aroe:CameraBase xmlns:aroe="http://www.adamroe.com/xsd/cameras.xsd"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.adamroe.com/xsd/cameras.xsd     C:\Users\Adam\Desktop\Cameras.xsd">
<aroe:Cameras>
    <aroe:Camera>
    <aroe:Make>SONY</aroe:Make>
    <aroe:Model>DSC-W130</aroe:Model>
    <aroe:Variable1>0.6352</aroe:Variable1>
    <aroe:Variable2>22.375</aroe:Variable2>
</aroe:Camera>
<aroe:Camera>
    <aroe:Make>Panasonic</aroe:Make>
    <aroe:Model>DMC-FX30</aroe:Model>
    <aroe:Variable1>0.8869</aroe:Variable1>
    <aroe:Variable2>24.73</aroe:Variable2>
</aroe:Camera>
<aroe:Camera>
    <aroe:Make>Olympus</aroe:Make>
    <aroe:Model>X450</aroe:Model>
    <aroe:Variable1>0.6003</aroe:Variable1>
    <aroe:Variable2>20.654</aroe:Variable2>
</aroe:Camera>
<aroe:Camera>
    <aroe:Make>Fujifilm</aroe:Make>
    <aroe:Model>FinePix S9600</aroe:Model>
    <aroe:Variable1>1.0024</aroe:Variable1>
    <aroe:Variable2>35.704</aroe:Variable2>
</aroe:Camera>
<aroe:Camera>
    <aroe:Make>Canon</aroe:Make>
    <aroe:Model>EOS 400D</aroe:Model>
    <aroe:Variable1>1.5143</aroe:Variable1>
    <aroe:Variable2>69.409</aroe:Variable2>
</aroe:Camera>
</aroe:Cameras>
</aroe:CameraBase>

The class is: 这堂课是:

public class Camera
{
    public string Make;
    public string Model;
    public double Variable1;
    public double Variable2;
}

Deserialize code: 反序列化代码:

public class PopulateXML
{
    public void DeserializeObject(string filenameXML)
    {
        Console.WriteLine("Reading with XmlReader");

        // Create an instance of the XmlSerializer specifying type and namespace.
        XmlSerializer serializer = new
        XmlSerializer(typeof(List<Camera>));

        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(filenameXML, FileMode.Open);
        XmlReader reader = new XmlTextReader(fs);

        // Declare an object variable of the type to be deserialized.
        List<Camera> i;

        // Use the Deserialize method to restore the object's state.
        i = (List<Camera>)serializer.Deserialize(reader);
    }
}

Main: 主要:

PopulateXML x = new PopulateXML();
        // Read a purchase order.
        x.DeserializeObject("Cameras.xml");

The exception is thrown on: i = (List)serializer.Deserialize(reader); 抛出异常:i =(List)serializer.Deserialize(reader);

XmlSerializer serializer = new XmlSerializer(typeof(Camera));

Stream fs = File.OpenRead(filenameXMLpath);

// Use the Deserialize method to restore the object's state.
Camera cam = serializer.Deserialize(fs) as Camera;

This will definitely work for you. 这绝对适合你。 In your case it was not working because you were type casting it to the incorrect data type ie, List. 在你的情况下,它无法正常工作,因为你输入的是不正确的数据类型,即List。 This error generally pops-up whenever developer type cast deserialized object to incorrect data type. 每当开发人员将反序列化的对象转换为不正确的数据类型时,通常会弹出此错误。

You could use LinqToXml and these extensions: http://searisen.com/xmllib/extensions.wiki to parse the Xml easily. 您可以使用LinqToXml和这些扩展: http ://searisen.com/xmllib/extensions.wiki轻松解析Xml。

XElement root = XElement.Load(file); // or .Parse(string)
List<Camera> cameras = root.GetEnumerable("Cameras/Camera", x => new Camera()
{
    Make = x.Get("Make", string.Empty),
    Model = x.Get("Model", string.Empty),
    Variable1 = x.Get<double>("Variable1", 0),
    Variable2 = x.Get<double>("Variable2", 0)
}).ToList();

PS I've tested it and it works on your xml. PS我已经测试了它,它适用于你的xml。

PSS Consider adding this attribute [DebuggerDisplay("{Make} - {Model}")] to your camera class to make viewing the list/array in the debugger nicer. PSS考虑将此属性[DebuggerDisplay("{Make} - {Model}")]到您的相机类,以便更好地查看调试器中的列表/数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM