代码之家  ›  专栏  ›  技术社区  ›  dkretz

我在反序列化期间得到一个异常

  •  2
  • dkretz  · 技术社区  · 5 年前

    我在运行以下代码时收到此消息:

    XML文档(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:-
    

    字符串:

    <?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>
    
    4 回复  |  直到 14 年前
        1
  •  1
  •   Anton Tykhyy    14 年前

    您需要关闭字符检查,如下所示:

    serializer.Deserialize (XmlReader.Create (inputStr, new XmlReaderSettings
        { CheckCharacters = false, }))
    
        2
  •  7
  •   Jon Skeet    14 年前

    除了缺少结束字段标记之外,还尝试将Unicode字符U+001B序列化…很遗憾,XML中不支持这一点。

    那是一个“逃避”的角色…你真的想从字符串开始吗?可能您只需在代码中添加验证(或剪裁)。

        3
  •  2
  •   Klaus Byskov Pedersen    14 年前

    有一个失踪的 </Fields> XML文档中的标记。

        4
  •  0
  •   dkackman Srinivas Kokkula    14 年前
    <FieldName>PRINTLINE00</FieldName> <FieldValue**>**DENIAL STATE</FieldValue>
    

    丢失AN<