反序列化字符串XML和错误:XML文档中存在错误(1,2)

时间:2015-05-08 16:34:02

标签: c# xml

我需要对字符串XML进行deseriliazing,但我有一个问题。

[XMLRoot]
public class OP
{
    [XmlElement]
    public string Auth;
    [XmlElement]
    public string User;
    [XmlElement]
    public string Password;
    [XmlElement]
    public string Client;
    [XmlElement]
    public string DownloadCode;
    [XmlElement]
    public string PartNumber;
    [XmlElement]
    public int FlexPO;
    [XmlArray]
    public string Terminals;
    [XmlElement]
    public string User;            
}

public void Test()
{
    var serializer = new XmlSerializer(typeof(OP));
    OP result;

    using (TextReader reader = new StringReader(XML))
    {
        result = (OP)serializer.Deserialize(reader);
    }

}

这是代码,这是XML:

<CreateManufactoryOrder xmlns="http://tempuri.org/">
    <op>
        <Auth>
            <User>User</User>
            <Password>Password</Password>
        </Auth>
        <Client>01425787000104</Client>
        <DownloadCode>0460.0001</DownloadCode>
        <PartNumber>M268-773-C4-BRA-3</PartNumber>
        <FlexPO>887614_364</FlexPO>
        <Terminals>
            <String>529-995-835</String>
            <String>529-995-836</String>
            <String>529-995-837</String>
            <String>529-995-838</String>
        </Terminals>
    </op>
</CreateManufactoryOrder>

因此,当我调试代码返回错误时:XML文档中存在错误(1,2)。 我已经尝试在很多方面改变代码,但没有运行。

2 个答案:

答案 0 :(得分:3)

您需要指定根元素名称和命名空间,将[XmlRoot]属性更新为:

[XmlRoot(ElementName = "CreateManufactoryOrder", Namespace= "http://tempuri.org/")]

我确认它现在在添加之后反序列化而没有问题。

答案 1 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\testxml";
        static void Main(string[] args)
        {
            CreateManufactoryOrder createManufacturyOrder = new CreateManufactoryOrder()
            {
                op = new Op(){
                    auth = new Auth(){
                        user = "User",
                        password = "Password"
                        },
                        client = "01425787000104",
                        downLoadCode  = "0460.0001",
                        partNumber = "M268-773-C4-BRA-3",
                        flexPO = "887614_364",
                        terminals = new List<Terminal>(){
                            new Terminal(){ m_String = "529-995-835"},
                            new Terminal(){ m_String = "529-995-836"},
                            new Terminal(){ m_String = "529-995-837"},
                            new Terminal(){ m_String = "529-995-838"}
                        }
                }
            };
            Test(createManufacturyOrder);
        }
        static public void Test(CreateManufactoryOrder createManufacturyOrder)
        {
 
            StreamWriter writer = new StreamWriter(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(CreateManufactoryOrder));

            serializer.Serialize(writer, createManufacturyOrder);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            string XML = File.ReadAllText(FILENAME);

            using (TextReader reader = new StringReader(XML))
            {
                CreateManufactoryOrder result = (CreateManufactoryOrder)serializer.Deserialize(reader);
            }

        }
    }
    [XmlRoot("CreateManufactoryOrder")]
    public class CreateManufactoryOrder
    {
        [XmlElement("op")]
        public Op op { get; set; }
    }
    [XmlRoot("op")]
    public class Op
    {
        [XmlElement("Auth")]
        public Auth auth { get; set; }
        [XmlElement("Client")]
        public string client;
        [XmlElement("DownloadCode")]
        public string downLoadCode { get; set; }
        [XmlElement("PartNumber")]
        public string partNumber { get; set; }
        [XmlElement("FlexPO")]
        public string flexPO { get; set; }
        [XmlElement("Terminals")]
        public List<Terminal> terminals { get; set; }
    }
    [XmlRoot("Auth")]
    public class Auth
    {
        [XmlElement("User")]
        public string user { get; set; }
        [XmlElement("Password")]
        public string password { get; set; }
    }
    public class Terminal
    {
        [XmlElement("String")]
        public string m_String { get; set; }
    }
}
​