I want to deseralize the following XML in C#
<Configuration>
<Parameters>
<Parameter AttrName1 = "AttrValue1"/>
<Parameter AttrName2 = "AttrValue2"/>
<Parameter AttrName3 = "AttrValue3"/>
</Parameters>
</Configuration>
I am having trouble because Attributes names and values are all differents for the same value.
Thank you so much.
You can use these classes to serialize/deserialize your xml. Note this is a feature in VS: Paste XML as Classes
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Configuration
{
private ConfigurationParameter[] parametersField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Parameter", IsNullable = false)]
public ConfigurationParameter[] Parameters
{
get
{
return this.parametersField;
}
set
{
this.parametersField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ConfigurationParameter
{
private string attrName1Field;
private string attrName2Field;
private string attrName3Field;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string AttrName1
{
get
{
return this.attrName1Field;
}
set
{
this.attrName1Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string AttrName2
{
get
{
return this.attrName2Field;
}
set
{
this.attrName2Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string AttrName3
{
get
{
return this.attrName3Field;
}
set
{
this.attrName3Field = value;
}
}
}
Related
I am getting this error when I run my code
System.InvalidOperationException
HResult=0x80131509
Message=HamiltonXML.Program is inaccessible due to its protection
level. Only public types can be processed.
Source=System.Xmlenter code here
StackTrace:
at System.Xml.Serialization.TypeDesc.CheckSupported()
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,
MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at HamiltonXML.Program.Main(String[] args) in C:\Users\Aube\source\repos\HamiltonProject\HamiltonProject\Program.cs:line 18
I've changed all classes to public and rechecked all of the code yet the problem still occurs. Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace HamiltonXML
{
class Program
{
static void Main(string[] args)
{
EnvironmentNodeConfigurationParameters encp = null;
string path = "C:/Users/Aube/Desktop/FreezerEnvironmentNodeParameters_Freezer1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(EnvironmentNodeConfigurationParameters));
StreamReader reader = new StreamReader(path);
encp = (EnvironmentNodeConfigurationParameters)serializer.Deserialize(reader);
reader.Close();
}
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class EnvironmentNodeConfigurationParameters
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurations[] complexDeviceParameterListField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("ComplexNodeConfigurations", IsNullable = false)]
public EnvironmentNodeConfigurationParametersComplexNodeConfigurations[] ComplexDeviceParameterList
{
get
{
return this.complexDeviceParameterListField;
}
set
{
this.complexDeviceParameterListField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurations
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurations[] complexNodeParameterListField;
private string subDeviceIDField;
private string subDeviceNameField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("NodeConfigurations", IsNullable = false)]
public EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurations[] ComplexNodeParameterList
{
get
{
return this.complexNodeParameterListField;
}
set
{
this.complexNodeParameterListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SubDeviceID
{
get
{
return this.subDeviceIDField;
}
set
{
this.subDeviceIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SubDeviceName
{
get
{
return this.subDeviceNameField;
}
set
{
this.subDeviceNameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurations
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParams environmentLinkParamsField;
private byte nodeIDField;
private string nodeNameField;
private string dependNodeNameField;
private string registerTypeField;
private byte startRegField;
private byte numRegsField;
private string nodeTypeField;
private string valueTypeField;
private string environmentLinkTypeField;
private string guidField;
private bool enabledField;
private string unitsField;
private bool monitoredField;
private byte reportItemField;
/// <remarks/>
public EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParams EnvironmentLinkParams
{
get
{
return this.environmentLinkParamsField;
}
set
{
this.environmentLinkParamsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte NodeID
{
get
{
return this.nodeIDField;
}
set
{
this.nodeIDField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NodeName
{
get
{
return this.nodeNameField;
}
set
{
this.nodeNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DependNodeName
{
get
{
return this.dependNodeNameField;
}
set
{
this.dependNodeNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string RegisterType
{
get
{
return this.registerTypeField;
}
set
{
this.registerTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte StartReg
{
get
{
return this.startRegField;
}
set
{
this.startRegField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte NumRegs
{
get
{
return this.numRegsField;
}
set
{
this.numRegsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NodeType
{
get
{
return this.nodeTypeField;
}
set
{
this.nodeTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ValueType
{
get
{
return this.valueTypeField;
}
set
{
this.valueTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string EnvironmentLinkType
{
get
{
return this.environmentLinkTypeField;
}
set
{
this.environmentLinkTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Guid
{
get
{
return this.guidField;
}
set
{
this.guidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool Enabled
{
get
{
return this.enabledField;
}
set
{
this.enabledField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Units
{
get
{
return this.unitsField;
}
set
{
this.unitsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool Monitored
{
get
{
return this.monitoredField;
}
set
{
this.monitoredField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public byte ReportItem
{
get
{
return this.reportItemField;
}
set
{
this.reportItemField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParams
{
private EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParamsKeyValuePair keyValuePairField;
/// <remarks/>
public EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParamsKeyValuePair KeyValuePair
{
get
{
return this.keyValuePairField;
}
set
{
this.keyValuePairField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class EnvironmentNodeConfigurationParametersComplexNodeConfigurationsNodeConfigurationsEnvironmentLinkParamsKeyValuePair
{
private string keyField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Key
{
get
{
return this.keyField;
}
set
{
this.keyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
}
The code is supposed to deserialize an XML file.
Have you tried changing your Program class to public?
namespace HamiltonXML
{
public class Program
{
This is my XML file.
<getMetadata>
<Project Name="Doors_Demo">
<Module FullPath="/Doors_Demo/Test_Module2">
<Attributes>
<Attribute name="TableType" type="TableType" />
<Attribute name="TableTopBorder" type="TableEdgeType" />
</Attributes>
</Module>
</Project>
</getMetadata>
I want to deserialize the above XML
Below is my code:
[XmlRoot("getMetadata")]
public class RootClass
{
public Project element_Project;
[XmlElement("Project")]
public Project Project
{
get { return element_Project; }
set { element_Project = value; }
}
}
public class Project
{
public string name;
[XmlAttribute("Name")]
public string Id
{
get { return name; }
set { name = value; }
}
}
public static void Main(string[] args)
{
RootClass obj = new RootClass();
XmlSerializer serializer = new XmlSerializer(typeof(RootClass));
using (FileStream stream = new FileStream(#"E:\getMetadata(4).xml", FileMode.Open))
{
RootClass myxml = (RootClass)serializer.Deserialize(stream);
Console.WriteLine(myxml.Project.name);
}
}
I want to deserialize my XML into a list, I am not able to access all inner elements and attributes inside the root element.
I want details of module element and its inner elements and tags into list which can be accessed.
Here is a little trick to generate classes from your XML automatically.
First, create a new empty class, name it for example TempXml.
Copy your XML to the clipboard and open the new empty class you just created.
Go to Visual Studio Edit menu then Paste Special and Paste XML as Classes:
This will generate the following code:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class getMetadata
{
private getMetadataProject projectField;
/// <remarks/>
public getMetadataProject Project
{
get
{
return this.projectField;
}
set
{
this.projectField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class getMetadataProject
{
private getMetadataProjectModule moduleField;
private string nameField;
/// <remarks/>
public getMetadataProjectModule Module
{
get
{
return this.moduleField;
}
set
{
this.moduleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class getMetadataProjectModule
{
private getMetadataProjectModuleAttribute[] attributesField;
private string fullPathField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Attribute", IsNullable = false)]
public getMetadataProjectModuleAttribute[] Attributes
{
get
{
return this.attributesField;
}
set
{
this.attributesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FullPath
{
get
{
return this.fullPathField;
}
set
{
this.fullPathField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class getMetadataProjectModuleAttribute
{
private string nameField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
Which should work fine with the XmlSerializer class.
You can clean up a little bit the generated output by removing the empty remarks, changing the name of the classes to use camel case (in this case you need to specify the real element name in the attribute as you were doing in your question) or move the classes to different files.
Hope it helps.
Could somebody help me generate C# class from XSD(sitemap with images)?
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
<loc>http://example.com/sample.html</loc>
<image:image>
<image:loc>http://example.com/image.jpg</image:loc>
</image:image>
<image:image>
<image:loc>http://example.com/photo.jpg</image:loc>
</image:image>
</url>
</urlset>
Here is my classes that were generated by xsd.exe tool:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,
Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
IsNullable = false)]
public partial class urlset
{
private System.Xml.XmlElement[] anyField;
private List<tUrl> urlField;
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
{
get { return this.anyField; }
set { this.anyField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("url")]
public List<tUrl> url
{
get { return this.urlField; }
set { this.urlField = value; }
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public partial class tUrl
{
private string locField;
private string lastmodField;
private tChangeFreq changefreqField;
private bool changefreqFieldSpecified;
private decimal priorityField;
private bool priorityFieldSpecified;
private System.Xml.XmlElement[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "anyURI")]
public string loc
{
get { return this.locField; }
set { this.locField = value; }
}
/// <remarks/>
public string lastmod
{
get { return this.lastmodField; }
set { this.lastmodField = value; }
}
/// <remarks/>
public tChangeFreq changefreq
{
get { return this.changefreqField; }
set { this.changefreqField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool changefreqSpecified
{
get { return this.changefreqFieldSpecified; }
set { this.changefreqFieldSpecified = value; }
}
/// <remarks/>
public decimal priority
{
get { return this.priorityField; }
set { this.priorityField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool prioritySpecified
{
get { return this.priorityFieldSpecified; }
set { this.priorityFieldSpecified = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlElement[] Any
{
get { return this.anyField; }
set { this.anyField = value; }
}
[System.Xml.Serialization.XmlElementAttribute(ElementName = "image", Type = typeof(image))]
public List<image> Images { get; set; }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public enum tChangeFreq
{
/// <remarks/>
always,
/// <remarks/>
hourly,
/// <remarks/>
daily,
/// <remarks/>
weekly,
/// <remarks/>
monthly,
/// <remarks/>
yearly,
/// <remarks/>
never,
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,
Namespace = "http://www.google.com/schemas/sitemap-image/1.1")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.google.com/schemas/sitemap-image/1.1",
IsNullable = false)]
public partial class image
{
private string locField;
private string captionField;
private string geo_locationField;
private string titleField;
private string licenseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(ElementName = "loc", DataType = "anyURI")]
public string loc
{
get { return this.locField; }
set { this.locField = value; }
}
/// <remarks/>
public string caption
{
get { return this.captionField; }
set { this.captionField = value; }
}
/// <remarks/>
public string geo_location
{
get { return this.geo_locationField; }
set { this.geo_locationField = value; }
}
/// <remarks/>
public string title
{
get { return this.titleField; }
set { this.titleField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "anyURI")]
public string license
{
get { return this.licenseField; }
set { this.licenseField = value; }
}
}
But I have problem with prefix 'image:', I could not sort out how to add this prefix to serialized xml. If I modify Element name by adding semicolon, then it do escape 'image_x003A_image'
I also used the xsd.exe tool and had the same issue. I resolved it as follows:
In the urlset class, add the following [XmlNamespaceDeclarations] property and create a constructor that adds the "image" xml namespace:
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public urlset()
{
xmlns.Add("image", "http://www.google.com/schemas/sitemap-image/1.1");
}
In my case I only needed a single image, so I added the following attribute to my image property in tUrl
[XmlElement(Namespace = "http://www.google.com/schemas/sitemap-image/1.1")]
public image image
{
get
{
return this.imageField;
}
set
{
this.imageField = value;
}
}
This then added the "image:" prefix to all my image elements. You should be able to do something similar for you image list to associate it with the namespace you created above.
This post seems a little old, but hope this helps someone experiencing similar headaches trying to utilize the image sitemap schema extension.
Hi am having some problems in XML serialization of XML attributes, following are some details of class file generated from XSD.
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/UpdatePolicy.xsd")]
public partial class policyClasses {
private policyClassesClass[] classField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("class")]
public policyClassesClass[] #class {
get {
return this.classField;
}
set {
this.classField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/UpdatePolicy.xsd")]
public partial class policyClassesClass {
private string company_idField;
private string deductable_rateField;
private string max_limitField;
private string class_nameField;
private string class_numberField;
private policyClassesClassCchi_class_number cchi_class_numberField;
private policyClassesClassAction_type action_typeField;
private bool action_typeFieldSpecified;
/// <remarks/>
public string company_id {
get {
return this.company_idField;
}
set {
this.company_idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]
public string deductable_rate {
get {
return this.deductable_rateField;
}
set {
this.deductable_rateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="positiveInteger")]
public string max_limit {
get {
return this.max_limitField;
}
set {
this.max_limitField = value;
}
}
/// <remarks/>
public string class_name {
get {
return this.class_nameField;
}
set {
this.class_nameField = value;
}
}
/// <remarks/>
public string class_number {
get {
return this.class_numberField;
}
set {
this.class_numberField = value;
}
}
/// <remarks/>
public policyClassesClassCchi_class_number cchi_class_number {
get {
return this.cchi_class_numberField;
}
set {
this.cchi_class_numberField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public policyClassesClassAction_type action_type {
get {
return this.action_typeField;
}
set {
this.action_typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool action_typeSpecified {
get {
return this.action_typeFieldSpecified;
}
set {
this.action_typeFieldSpecified = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/UpdatePolicy.xsd")]
public enum policyClassesClassCchi_class_number {
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("1")]
Item1,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("2")]
Item2,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("3")]
Item3,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("4")]
Item4,
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/UpdatePolicy.xsd")]
public enum policyClassesClassAction_type {
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("10")]
Item10,
/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("1")]
Item1,
I managed to generate XML based on this class by serialization, but not having any idea to add the XML attribute to the XML generated, i supposed to get an output as below.
<classes>
<class action_type="10">
<company_id>999</company_id>
<deductable_rate>20</deductable_rate>
<max_limit>800</max_limit>
<class_name>Class A</class_name>
<class_number>1</class_number>
<cchi_class_number>2</cchi_class_number>
</class>
how to add the attribute to the element <class action_type = "10">
from /// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public policyClassesClassAction_type action_type {
get {
return this.action_typeField;
}
set {
this.action_typeField = value;
}
}
Please somebody help me on this to manage my deadlines, also tell me if anything is not clear for the valuable responses
Joe
Notice that the generated class is marked partial.
Add another class right next to the generated one in your project and give it a similar name, such as GeneratedClasses.addon.cs. Add this in your new file:
public partial class policyClasses {
[System.Xml.Serialization.XmlAttribute]
public policyClassesClassAction_type action_type { get;set;}
}
I am having a problem with serializing an object in C#. When the application goes to serialize the object, certain fields get serialized but others do not. In the following code:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ACORDInsuranceSvcRqHomePolicyQuoteInqRq
{
private string rqUIDField;
private System.DateTime transactionRequestDtField;
private System.DateTime transactionEffectiveDtField;
private string curCdField;
/// <remarks/>
public string RqUID
{
get
{
return this.rqUIDField;
}
set
{
this.rqUIDField = value;
}
}
/// <remarks/>
public string CurCd
{
get
{
return this.curCdField;
}
set
{
this.curCdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnore()]
public System.DateTime TransactionRequestDt
{
get
{
return this.transactionRequestDtField;
}
set
{
this.transactionRequestDtField = value;
}
}
/// <remarks/>
[XmlElement("TransactionRequestDt")]
public string TransactionRequestDtString
{
get
{
return String.Format("{0:yyyy-MM-dd}", this.TransactionRequestDt);
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnore]
public System.DateTime TransactionEffectiveDt
{
get
{
return this.transactionEffectiveDtField;
}
set
{
this.transactionEffectiveDtField = value;
}
}
/// <remarks/>
[XmlElement("TransactionEffectiveDt")]
public string TransactionEffectiveDtString
{
get
{
return String.Format("{0:yyyy-MM-dd}", this.TransactionEffectiveDt);
}
}
}
you can see that the Fields/Accessors RqUID and CurCd get called but TransactionRequestDtString and TransactionEffectiveDtString do not. I need all of these to be serialized. Thanks!
If they need to be xml serialized they need a public get and set.
Try changing your code to this:
[ReadOnly(true)]
[XmlElement("TransactionRequestDt")]
public string TransactionRequestDtString
{
get
{
return String.Format("{0:yyyy-MM-dd}", this.TransactionRequestDt);
}
set{}
}`
The ReadOnly attribute will not let anyone change it.
Possible answer see: Serializing private member data
I was facing the same issue with some properties(which are nullable) , i FIXED it by using : [XmlElement(IsNullable = true)] decorator
public class Person
{
[XmlElement(IsNullable = true)]
public string Name { get; set; }
}