2

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
myWorld
  • 49
  • 1
  • 11

4 Answers4

7

Aside from the fact that you're missing a closing Fields tag, you've also tried to serialise Unicode character U+001B... which unfortunately isn't supported in XML.

That's an "escape" character... did you really want it in the string to start with? It could be that you can just add validation (or trimming) in your code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • please ignore all those fromatting, everthing is well formed – myWorld May 21 '10 at 12:27
  • I want to know if StringWriter and UTF16 has some prob related to this exception. any other way we can Encoding would solve this prob – myWorld May 21 '10 at 12:29
  • Jon, U+001B might not be supported by XML, but [it is supported](http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.checkcharacters.aspx) by .NET's XML serializer. – Anton Tykhyy May 24 '10 at 06:27
  • @Anton: True (and I didn't know that before - why did you delete your answer). I would suggest that creating invalid XML isn't a great idea though :( – Jon Skeet May 24 '10 at 06:43
  • Can you please suggest a way where i can serialize something of type String and i dont get such exception(beacuse of the escape chars) and also have a handle on the type i.e., use UTF-8 rather than the defaultly generated UTF-16 – myWorld May 24 '10 at 06:49
  • @starz26: As Anton suggested, you *could* set XmlReaderSettings.CheckCharacters to false when you deserialize, but then you end up with invalid XML. To generate UTF-8 instead of UTF-16, you need to use a StringWriter which advertises itself as being UTF-8 - see http://stackoverflow.com/questions/1564718/using-stringwriter-for-xml-serialization/1564727#1564727 – Jon Skeet May 24 '10 at 06:55
2

There's a missing </Fields> tag in your xml document.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
1

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

serializer.Deserialize (XmlReader.Create (inputStr, new XmlReaderSettings
    { CheckCharacters = false, }))
Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56
0
<FieldName>PRINTLINE00</FieldName> <FieldValue**>**DENIAL STATE</FieldValue>

missing an <

dkackman
  • 15,179
  • 13
  • 69
  • 123