使用xsd.exe生成类后仅反序列化Adres报错求助
Hey there, let's figure out why you're hitting that "error in XML document (1, 40)" message when trying to deserialize just the Adres class. Here are the most common issues and actionable fixes to try:
1. Root Element Mismatch
By default, XmlSerializer expects the XML's root element to exactly match the name and structure of the Adres class. But if your 15MB XML has a parent root element (like a container wrapping the Adres node), this will throw an immediate error.
Fix: Target the Adres Node Directly
Instead of deserializing the entire document, use an XmlReader to navigate to the <Adres> element first, then deserialize that specific node:
XmlSerializer serializer = new XmlSerializer(typeof(Adres)); using (StringReader reader = new StringReader(xmlDocumentText)) using (XmlReader xmlReader = XmlReader.Create(reader)) { // Skip until we locate the Adres element while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "Adres") { Adres info = (Adres)serializer.Deserialize(xmlReader); // Work with your deserialized Adres object here break; } } }
2. Namespace Mismatch
When you generated classes with xsd.exe, it likely added XML namespace attributes to your Adres class (look for [XmlRoot(Namespace = "some-namespace-uri")] above the class definition). If your XML's <Adres> element uses a different namespace (or no namespace at all), this causes a mismatch.
Fixes:
- Adjust class attributes: If your XML has no namespace, remove the
Namespaceparameter from theXmlRootattribute on theAdresclass. - Explicitly set the namespace in the serializer: If the XML uses a namespace that doesn't match the class's, override it when creating the serializer:
XmlRootAttribute rootAttr = new XmlRootAttribute("Adres") { Namespace = "http://your-xml-namespace-uri" }; XmlSerializer serializer = new XmlSerializer(typeof(Adres), rootAttr);
3. Class-to-XML Structure Mismatch
It’s possible the Adres class generated by xsd.exe doesn’t perfectly align with the structure of the <Adres> element in your XML—for example, missing properties, incorrect data types, or unexpected extra elements.
Fix:
- Get precise error details: Catch the
InvalidOperationExceptionand check itsInnerException—it will tell you exactly what’s mismatched (e.g., "The element 'Postcode' was not expected").try { XmlSerializer serializer = new XmlSerializer(typeof(Adres)); using (StringReader reader = new StringReader(xmlDocumentText)) { Adres info = (Adres)serializer.Deserialize(reader); } } catch (InvalidOperationException ex) { // Log or inspect this message for exact issues Console.WriteLine($"Deserialization failed: {ex.InnerException.Message}"); } - Validate XML against your XSD: Use tools like Visual Studio’s built-in XML validation or
xmllintto confirm your XML conforms to the schema you used to generate the classes.
Quick Debug Hack
Since your XML is huge, extract a single <Adres> node (with all its child elements) into a small test XML file. Use this smaller file to iterate on your deserialization code—it’ll save you time by avoiding loading the full 15MB document every test run.
内容的提问来源于stack exchange,提问作者FizzOlson

