Ich habe die Beschreibung meiner Frage so geändert, dass sie einen reproduzierbaren Fall enthält.
Meine Stammelementklasse sieht so aus (geändert von der generierten Klasse):
Code: Select all
namespace TestXmlSerializer
{
///
///
This is the root object of the XML data. It defines the entire DF
/// This is the root object of the XML data. It defines the entire DF
///
[System.ComponentModel.DescriptionAttribute("This is the root object of the XML data. It defines the entire DF")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.662.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("dfType", Namespace = "http://www.company.com/a661")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("a661_df", Namespace = "http://www.company.com/a661")]
public partial class DfType : ElementType
{
///
/// Initializes a new instance of the class.
///
public DfType()
{
this._a661_Layer = new System.Collections.ObjectModel.Collection();
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
private System.Collections.ObjectModel.Collection _a661_Layer;
[System.ComponentModel.DataAnnotations.RequiredAttribute()]
[System.Xml.Serialization.XmlElementAttribute("a661_layer")]
public System.Collections.ObjectModel.Collection A661_Layer
{
get
{
return this._a661_Layer;
}
private set
{
this._a661_Layer = value;
}
}
///
/// this is the name of the DF. It corresponds to the name of the file (not including file extension). Present in the XML Definition File only
/// To help enforce sensible names in data.
/// Minimum length: 1.
/// Maximum length: 128.
///
[System.ComponentModel.DescriptionAttribute("this is the name of the DF. It corresponds to the name of the file (not including" +
" file extension). Present in the XML Definition File only")]
[System.ComponentModel.DataAnnotations.MinLengthAttribute(1)]
[System.ComponentModel.DataAnnotations.MaxLengthAttribute(128)]
[System.Xml.Serialization.XmlAttributeAttribute("name")]
public string Name { get; set; }
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.662.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("elementType", Namespace = "http://www.company.com/a661")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DfType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(LayerType))]
public partial class ElementType
{
[System.Xml.Serialization.XmlIgnoreAttribute()]
private System.Collections.ObjectModel.Collection _any;
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Collections.ObjectModel.Collection Any
{
get
{
return this._any;
}
private set
{
this._any = value;
}
}
///
/// Gets a value indicating whether the Any collection is empty.
///
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AnySpecified
{
get
{
return (this.Any.Count != 0);
}
}
///
/// Initializes a new instance of the class.
///
public ElementType()
{
this._any = new System.Collections.ObjectModel.Collection();
this._anyAttribute = new System.Collections.ObjectModel.Collection();
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
private System.Collections.ObjectModel.Collection _anyAttribute;
[System.ComponentModel.DataAnnotations.RequiredAttribute()]
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Collections.ObjectModel.Collection AnyAttribute
{
get
{
return this._anyAttribute;
}
private set
{
this._anyAttribute = value;
}
}
}
[System.ComponentModel.DescriptionAttribute("Defines a layer")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.662.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("layerType", Namespace = "http://www.company.com/a661")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class LayerType : ElementType
{
///
/// Initializes a new instance of the class.
///
public LayerType()
{
}
///
/// present in the XML Definition variant of the file only. May be used for descriptive or other purposes.
/// To help enforce sensible names in data.
/// Minimum length: 1.
/// Maximum length: 128.
///
[System.ComponentModel.DescriptionAttribute("present in the XML Definition variant of the file only. May be used for descripti" +
"ve or other purposes.")]
[System.ComponentModel.DataAnnotations.MinLengthAttribute(1)]
[System.ComponentModel.DataAnnotations.MaxLengthAttribute(128)]
[System.Xml.Serialization.XmlAttributeAttribute("name")]
public string Name { get; set; }
}
}
Code: Select all
namespace TestXmlSerializer
{
internal class Program
{
static void Main(string[] args)
{
if (args.Length == 1 )
{
LoadDefinitionFile(args[0]);
} else
{
Console.WriteLine("program ");
}
}
private static DfType LoadDefinitionFile(string path)
{
// Read df as text
string df = File.ReadAllText(path);
// Dump bytes of patched df file
byte[] dfBytes = Encoding.ASCII.GetBytes(df);
// Deserialize
// Load input file
using MemoryStream memoryStream = new MemoryStream(dfBytes);
// Create Reader
XmlReader xmlReader = new XmlTextReader(memoryStream);
// Load xml Model
XmlSerializer serializer = new XmlSerializer(typeof(DfType));
// Deserialization
return (DfType)serializer.Deserialize(xmlReader);
}
}
}
- mit xmlns
Code: Select all
- ohne xmlns
Code: Select all
Code: Select all
Unhandled exception. System.InvalidOperationException: There is an error in XML document (2, 2).
---> System.InvalidOperationException: was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderDfType.Read5_a661_df()
Ich habe einen Java-Editor mit Geschichte. Am einfachsten ist es, unsere XML-Dateien mit dem C#-Generator funktionieren zu lassen.
Wie kann der Root-Namespace bei der Deserialisierung mit XmlSerializer richtig überschrieben werden?
Mobile version