1

Why use xsd.exe tool to create class for XSD schema and then use serializer to create XML file, when you can use XDocument.

Should I be always using XDocument and manually create the XML document by nesting XElement elements, or is there something I am not seeing that makes using XSD schema and XmlSerializer better.

9
  • 3
    "better" is subjective and depends on what you're measuring; XmlSerializer is more efficient in that it doesn't need to create a DOM - it is based on XmlReader/XmlWriter IIRC - and it is usually easier to get correctly working deserialization code by not writing it yourself - deserialization is deceptively complex in the edges; but if the DOM-based approach works fine for your data, and you don't need to formally define it in a separate schema: fine, have fun! Do what works! Commented Sep 10, 2024 at 10:11
  • 1
    When you use XDocument, you need to keep referring back to the structure of the XML document when you access children / properties, so you know what they're called and what type they have. With xsd.exe, you get generated C# types which already have all of the right properties / children on them, with the right types -- you don't need to keep looking that information up, as it's right there in the type system. It also means that if your schema is changed, you get a compile-time error, rather than having to find all the places where XDocument is used to reference a now-removed property Commented Sep 10, 2024 at 10:34
  • 1
    "did not create new class from it again though" there you go. Of course you need to run xsd.exe again when you change the XSD, otherwise the generated C# classes won't match the updated XSD. You can separately validate that an XML document which you receive matches an XSD, see e.g. stackoverflow.com/a/752774/1086121 Commented Sep 10, 2024 at 11:30
  • 1
    @Nikola If you generated C# classes from the XSD, then the XML generated from those C# classes should match the XSD -- there's no need to check that again, normally. However if you're receiving XML, then it might be worth validating that against an XSD Commented Sep 10, 2024 at 11:32
  • 1
    That said, sometimes you need to work with someone else's XML document, and either there's no XSD or the generated C# classes are far more complex than you need. Or sometimes you're just doing something very simple and it's not worth writing an XSD. Or sometimes the XML does something really annoying which doesn't map well to C# classes. In those cases, using XDocument is often the better choice. Commented Sep 10, 2024 at 11:33

1 Answer 1

2

XDocument and XmlSerializer/xsd.exe are useful for different sorts of things.

When you're dealing with a large XML document, you should really have an XSD schema. This is an agreement between all parties on what the structure of the XML document should be, what properties / children / data types, it has etc.

If you have an XSD, then you might as well use it to generate C# classes. You could hand-write XDocument code, but then you need to keep referring back to the XSD to see what properties / children you should be accessing, and what their data types are. If the XSD changes, you have to go and manually search through your XDocument code looking for things which need to be changed.

If instead you generate C# classes, then the properties you can access, and their data types, are right there in the type system: you don't need to refer to the XSD. If the XSD changes, you can re-generate your C# classes and get compiler errors in places you need to change.

If you have an XSD, you should also validate that XML documents you receive match that XSD, see e.g. this answer.

XmlSerializer is often also more efficient than XDocument.

However, sometimes you need to work with someone else's XML document, and either there's no XSD or the generated C# classes are far more complex than you need. Or sometimes you're just doing something very simple and it's not worth writing an XSD. Or sometimes the XML does something really annoying which doesn't map well to C# classes. In those cases, using XDocument is often the better choice.

(The one thing you should avoid at all costs is XmlDocument -- that does the same thing as XDocument, but much worse.)

Sign up to request clarification or add additional context in comments.

6 Comments

XML serializer is very slow. XDocument is much faster.
@jdweng That is not true. XDocument has to load the entire DOM into memory, in a structured way that it can perform queries against.
"If you have an XSD, then you might as well use it to generate C# classes." Not necessarily. If the XSD is subject to frequent change, or if you have to handle multiple versions, then the data binding approach is definitely a bad idea. It's also probably a bad idea if the schema allows lots of variability in the data.
@MichaelKay You're saying if the schema changes a lot, it's best not to have a mechanism which tells you when breaking changes are made?
@MichaelKay I guess it depends on how much of the document you care about. If it's a large proportion, i.e. a schema change is likely to break you either way, then an easy-to-find compile-time error sounds a lot better than one which only pops up at runtime! XmlSerializer tends to be fairly forgiving in my experience: it has no way to specify that things are required, XML doesn't distinguish between children and arrays anyway, etc
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.