0

I have xml file:

<TestType>
  <Names>
    <Name>
      <ID>0</ID>
    </Name>
    <Name>
      <ID>1</ID>
    </Name>
    <Name>
      <ID>2WRONG</ID>
    </Name>
    <Name>
      <ID>3</ID>
    </Name>
  </Names>
</TestType>

2 simple classes

public class Name
{
    [XmlElement("ID")]
    public int Id { get; set; }
}
[XmlRoot("TestType")]
[Serializable]
public class TestType
{
     public Collection<Name> Names { get; set; } 
}

and try to deserialize xml file:

private static void ReadXml()
        {

            var book = new TestType(){Names = new Collection<Name>()};
            var reader =
                new XmlSerializer(typeof(TestType));

            var file = new XmlTextReader(
                @"c:\temp\stest.xml");
            try
            {
                book = (TestType)reader.Deserialize(file);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
            file.Close();
            Console.WriteLine(book.Names.Count);
            Console.ReadKey();
        }

as expected in default behaviour, deserialize throw InvalidOperationException, "invalid input string" on element where id = 2WRONG(is string,not int), and deserialization process abort. In book.Names collection zero elements.

How i can do this:

1) deserialize process dont abort on wrong entry, his throw exception, i catch him and process continue. 2) book.Names collection after deserialization contain all right elements((id=0,1,3) in this case)

PS: I tried to mix validation and deserialization, yes. I do this because deserialization is very fast,this important for me. If i do this wrong - show me the way please.

har07
  • 88,338
  • 12
  • 84
  • 137

1 Answers1

0

I've been were you were exactly a few months ago when I was trying to figure out how serialization/deserialization with xml docs worked.

You have some options when it comes to deserialize information into code.

1- Passing from XML to a simple class

Create a class which will contain propreties based on the document you're trying to read. Doing so, you'll know exactly what information you want. Here's an example of how to use TextReader and XmlSerializer

MyClass object = new MyClass();
string filename =  "C/path/to/file.xml";
TextReader fileReader =  new StreamReader(filename); // the constructor of textreader does not take parameters and inherits from streamreader class 
XmlSerializer serializer= new  XmlSerializer(typeOf(MyClass));
//Deserialize the xml document 
object = (MyClass) serializer.Deserialize(reader);
object.Close(); //close the stream of data!

2- Using LINQ LINQ is a very powerful tool when it comes to make different kind of queries. This process is efficient and clean. I'm going to have to refer you to this post which has an excellent example : How to deserialize xml using Linq?

3- If option 1 and 2 weren't enough for you, I can suggest to see this : How to Deserialize XML document

Hope I have help you out a bit ! :)

Community
  • 1
  • 1
Kevin Avignon
  • 2,853
  • 3
  • 19
  • 40