简体   繁体   中英

create xml from object without setting values in it c#

I want to convert ac# class to an xml file without declaring default values in it. If I declare values on every propery in the class I got it to work and the XML has all the properties in it. The primarydata is my class with properties in it.

 var pD = new PrimaryData();
 XmlSerializer serializerPrimaryData = new XmlSerializer(typeof(PrimaryData));
 serializerPrimaryData.Serialize(File.Create(xmlLocation), pD,ns);

But I dont want to declare any values.

If i run this code I get just:

 <?xml version="1.0"?>
<PrimaryData />

I don't get the properties in the class as you can see. So how can I get the properties in the class without declaring them to a default value? Any suggestions?

I have followed this guide: https://codehandbook.org/c-object-xml/ But he is declaring default values to his class.

public class PrimaryData
{
    public PrimaryData();

    public string BatchId { get; set; }
    public CurrentOperation CurrentOperation { get; set; }
    public Heat Heat { get; set; }
    public string MaterialId { get; set; }
    public List<Operation> Operations { get; set; }
    public OrderInfo OrderInfo { get; set; }
    public Plate Plate { get; set; }
}

I have no clue what you are trying to do ...

XML serialization just makes a mirror of the object in different structure (aka XML - object will be represented by structured text).

If your class has no properties, nothing will be serialized to XML but only the base empty object.

My suggestion is to make a class with Dictionary<string, object> as property. Then you will be able to write into the Dictionary and serialize it.

But, it has some drawbacks. XML Serialization (if I remember correctly) does not support Dictionaries, so I would go with DataContract instead.

That might work :)

[DataContract]
public class PrimaryData
{
    [DataMember(Name = "Data", Order = 0, IsRequired = true)]
    public Dictionary<string, object> Data { get; set; }
}

Just set default values.

public class Employee
{
    public string FirstName { get; set; } = "";
    public string LastName { get; set; } = "";
}

public static void Main(string[] args)
{
    var employee = new Employee();
    var sw = new StringWriter();
    var se = new XmlSerializer(employee.GetType());
    var tw = new XmlTextWriter(sw);
    se.Serialize(tw, employee);
    Console.WriteLine(sw.ToString());
    Console.Read();
}

Result

<?xml version="1.0" encoding="utf-16"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <FirstName />
    <LastName />
</Employee>

Second solution is to set XmlElement(IsNullable = true)

public class Employee
{
    [XmlElement(IsNullable = true)]
    public string FirstName { get; set; }
    [XmlElement(IsNullable = true)]
    public string LastName { get; set; }
}

You should add this annotation to your properties:

[XmlElement(IsNullable = true)]
public string Prop { get; set; }

The result in your xml should be something like this:

<Prop xsi:nil="true" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM