I have been trying to deserialize an XML file and pull out some data from StructuredText element of the XML. So far, namespace have been causing issues.
I generated the classes with xsd from a schema but the classes have no namespace specified. I need to be able to use different namespaces so having them vary would be nice.
I tried defining the namespace in the root attribute but like that the deserialization only returns null. If I do not specify one I get an error from the deserializer on the StructuredText element.
private StructuredText_T DeserializeStructuredText(string filePath) {
StructuredText_T structuredText;
XmlSerializer serializer = new XmlSerializer(typeof(TIAOpennessAppSclXsd.StructuredText_T),
new XmlRootAttribute("StructuredText")
{
Namespace = "http://www.siemens.com/automation/Openness/SW/NetworkSource/StructuredText/v4",
IsNullable = false
});
XmlReaderSettings settings = new XmlReaderSettings
{
CheckCharacters = false // Ignore invalid characters
};
using (XmlReader reader = XmlReader.Create(filePath, settings))
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "StructuredText")
{
Console.WriteLine($"Deserialize {reader.NodeType} named {reader.Name}");
structuredText = (StructuredText_T)serializer.Deserialize(reader);
return (structuredText);
}
}
return null;
}
The deserialization only works if I change the XSD generated classes and specify the namespace inside the RootAttribute there like so:
[System.Xml.Serialization.XmlRootAttribute("StructuredText", Namespace="http://www.siemens.com/automation/Openness/SW/NetworkSource/StructuredText/v4", IsNullable=false)]
But this is something I want to avoid. First because there is a lot of classes and second because I need flexibility.
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("StructuredText", Namespace="", IsNullable=false)]
public partial class StructuredText_T {
private object[] itemsField;
private int uIdField;
private bool uIdFieldSpecified;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Access", typeof(Access_T))]
[System.Xml.Serialization.XmlElementAttribute("Blank", typeof(Blank_T))]
[System.Xml.Serialization.XmlElementAttribute("Comment", typeof(Comment_T))]
[System.Xml.Serialization.XmlElementAttribute("LineComment", typeof(LineComment_T))]
[System.Xml.Serialization.XmlElementAttribute("NewLine", typeof(NewLine_T))]
[System.Xml.Serialization.XmlElementAttribute("Parameter", typeof(Parameter_T))]
[System.Xml.Serialization.XmlElementAttribute("Text", typeof(Text_T))]
[System.Xml.Serialization.XmlElementAttribute("Token", typeof(Token_T))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public int UId {
get {
return this.uIdField;
}
set {
this.uIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool UIdSpecified {
get {
return this.uIdFieldSpecified;
}
set {
this.uIdFieldSpecified = value;
}
}
}
What am I missing?