我得到反序列化过程异常异常、过程、序列化

2023-09-04 03:13:21 作者:沉淀

运行低于code,当我得到这个消息:

  

有一个在XML文档(6,22)。

错误

 命名空间N1
{
    公共类InputEntry
    {
        //字段名是一类谓是从具有复杂类型的名称和值的XSD生成
        私人场域;
        公共字段[]字段
        {
            {返回this.fields; }
            集合{this.fields =价值; }
        }

        公共字符串字段名
        {
           {返回this.fieldName;}
           集合{this.fieldName =价值; }
        }

       公共字符串fieldValue方法
       {
        {返回this.fieldValue; }
        集合{this.fieldValue =值;}
       }
    }

    大众B级
    {
        公共无效方法1()
        {
            InputEntry inputEntry =新InputEntry();
            //我们一些如何从用户得到的值,并将其分配给
            场F1 =新的领域();
            f1.FieldName =PRINTLINE00;
            f1.FieldValue =拒绝状态;
        }

        私人无效方法2(InputEntry inputEntry)
        {
            System.Xml.Serialization.XmlSerializer串行=新System.Xml.Serialization.XmlSerializer(typeof运算(InputEntry));
            System.IO.StringWriter inputStr =新System.IO.StringWriter(CultureInfo.InvariantCulture);
            serializer.Serialize(inputStr,inputEntry);
            字符串ipEntry = inputStr.ToString();
            Method3(ipEntry);
        }

        私人无效Method3(字符串ipEntry)
        {
            System.Xml.Serialization.XmlSerializer串行=新System.Xml.Serialization.XmlSerializer(typeof运算(InputEntry));
            System.IO.StringReader inputStr =新System.IO.StringReader(ipEntry);
            InputEntry inputEntry =(InputEntry)serializer.Deserialize(inputStr);

        }
    }
}
     //当客户端configues输入数据
     //<字段名> PRINTLINE00< /字段名> < fieldValue方法>剥夺态LT; / fieldValue方法>中
     //我得到一个异常时,相同的数据deserialised
     //例外是: - 
 

字符串:

 < XML版本=1.0编码=UTF-16&GT?;
< InputEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <场>
    <现场>
      <字段名> PRINTLINE00< /字段名>
      < fieldValue方法>&安培;#X1B;剥夺状态217< / fieldValue方法>
    < /场>
  < /场>
< / InputEntry>
 

解决方案 反序列化的那些事儿

您需要关闭的性格检查,是这样的:

  serializer.Deserialize(XmlReader.Create(inputStr,新XmlReaderSettings
    {CheckCharacters =假,}))
 

I get this message when running the code below:

There is an error in XML document (6, 22).

namespace N1
{
    public class InputEntry
    {
        //FieldName is a class tht is generated from an XSD which has complex type Name and value
        private Field fields;
        public Field[] Fields
        {
            get { return this.fields; }
            set { this.fields= value; }
        }

        public string FieldName
        {
           get{return this.fieldName;}
           set { this.fieldName = value; }
        }

       public string FieldValue
       {
        get {return this.fieldValue; }
        set {this.fieldValue = value;}
       }
    }

    public class B
    {
        public void Method1()
        {
            InputEntry inputEntry = new InputEntry();
            //we some how get the values from user and assign them to
            Field f1 = new Field();
            f1.FieldName = "PRINTLINE00";
            f1.FieldValue = "DENIAL STATE" ;
        }

        private void Method2(InputEntry inputEntry)
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(InputEntry));
            System.IO.StringWriter inputStr = new System.IO.StringWriter(CultureInfo.InvariantCulture);
            serializer.Serialize(inputStr, inputEntry);
            string ipEntry = inputStr.ToString();
            Method3(ipEntry);
        }

        private void Method3(string ipEntry)
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(InputEntry));
            System.IO.StringReader inputStr = new System.IO.StringReader(ipEntry);
            InputEntry inputEntry = (InputEntry)serializer.Deserialize(inputStr);

        }
    }
}
     // when client configues input data as
     // <FieldName>PRINTLINE00</FieldName> <FieldValue>DENIAL STATE</FieldValue>,
     //I get an exception when the same data deserialised
     //Exception is:-

String:

<?xml version="1.0" encoding="utf-16"?>
<InputEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Fields>
    <Field>
      <FieldName>PRINTLINE00</FieldName>
      <FieldValue>&#x1B;DENIAL STATE 217</FieldValue>
    </Field>
  </Fields>
</InputEntry>

解决方案

You need to turn off character checking, something like this:

serializer.Deserialize (XmlReader.Create (inputStr, new XmlReaderSettings
    { CheckCharacters = false, }))