Deserialisieren Sie mit XmlSerializer eine XML-Datei, für die möglicherweise xmlns festgelegt ist oder nichtC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Deserialisieren Sie mit XmlSerializer eine XML-Datei, für die möglicherweise xmlns festgelegt ist oder nicht

Post by Anonymous »

Ich habe C#-Klassen mit targetNamespace generiert. Jetzt möchte ich diesen Typ mit einer XML-Datei verwenden, für die möglicherweise das xmlns-Stammattribut festgelegt ist oder nicht.
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; }
}

}
Ich habe den folgenden Deserialisierungscode ausprobiert:

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);
}
}
}
Ich habe das Beispielprogramm mit den folgenden Beispiel-XML-Dateien ausprobiert:
  • mit xmlns

Code: Select all




  • ohne xmlns

Code: Select all




Wenn ich das Beispielprogramm mit 1) ausführe, scheint es zu funktionieren, aber wenn ich es mit 2) ausführe, wird eine Ausnahme ausgelöst:

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()
Die Geschichte hinter dieser Frage ist, dass sie in der Java-Welt mit SAXParser funktioniert, wo Sie das Schema angeben können, dem Ihre XML-Datei folgen soll.
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?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post