Serializing a class under a different element name - c#

I have this class:
[XmlRoot(ElementName ="Lesson")]
public class LessonOld
{
public LessonOld()
{
Students = new List<string>();
}
public string Name { get; set; }
public DateTime FirstLessonDate { get; set; }
public int DurationInMinutes { get; set; }
public List<string> Students { get; set; }
}
And I'm using this code to serialize it:
TextWriter writer = new StreamWriter(Path.Combine(UserSettings, "Lessons-temp.xml"));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<LessonOld>));
xmlSerializer.Serialize(writer, tempList);
writer.Close();
(Note that that is a List<LessonOld>)
Here is my resulting XML:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLessonOld xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LessonOld>
<FirstLessonDate>0001-01-01T00:00:00</FirstLessonDate>
<DurationInMinutes>0</DurationInMinutes>
<Students />
</LessonOld>
</ArrayOfLessonOld>
I would like to change it to serialize as <ArrayOfLesson and <Lesson> for the XML elements. Is this possible? (As you can see, I've already tried using [XmlRoot(ElementName ="Lesson")])

You're almost there. Use:
[XmlType(TypeName = "Lesson")]
instead of
[XmlRoot(ElementName = "Lesson")]
Of course you can test it easily; your code with the above change
[XmlType(TypeName = "Lesson")]
public class LessonOld
{
public LessonOld()
{
Students = new List<string>();
}
public string Name { get; set; }
public DateTime FirstLessonDate { get; set; }
public int DurationInMinutes { get; set; }
public List<string> Students { get; set; }
}
class Program
{
static void Main(string[] args)
{
TextWriter writer = new StreamWriter(Path.Combine(#"C:\Users\Francesco\Desktop\nanovg", "Lessons-temp.xml"));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<LessonOld>));
xmlSerializer.Serialize(writer, new List<LessonOld> { new LessonOld() { Name = "name", DurationInMinutes = 0 } });
writer.Close();
}
}
produces this
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLesson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Lesson>
<Name>name</Name>
<FirstLessonDate>0001-01-01T00:00:00</FirstLessonDate>
<DurationInMinutes>0</DurationInMinutes>
<Students />
</Lesson>
</ArrayOfLesson>
As I've seen it, XmlRoot works fine when you want to serialize a single object.
Consider this code, derived from yours:
[XmlRoot(ElementName = "Lesson")]
public class LessonOld
{
public LessonOld()
{
Students = new List<string>();
}
public string Name { get; set; }
public DateTime FirstLessonDate { get; set; }
public int DurationInMinutes { get; set; }
public List<string> Students { get; set; }
}
class Program
{
static void Main(string[] args)
{
TextWriter writer = new StreamWriter(Path.Combine(#"C:\Users\Francesco\Desktop\nanovg", "Lessons-temp.xml"));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LessonOld));
xmlSerializer.Serialize(writer, new LessonOld() { Name = "name", DurationInMinutes = 0 });
writer.Close();
}
}
It will output
<?xml version="1.0" encoding="utf-8"?>
<Lesson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>name</Name>
<FirstLessonDate>0001-01-01T00:00:00</FirstLessonDate>
<DurationInMinutes>0</DurationInMinutes>
<Students />
</Lesson>

Related

c# how to save list to xml with list child List<string, Class>

How to convert this:
public class Person
{
public string Name{ get; set; }
public string Age{ get; set; }
}
public class SectionList
{
public List<Person> PersonList { get; set; }
public string SectionName { get; set; }
}
public static List<SectionList> sectionList = new List<SectionList>();
into this XML:
<SectionList>
<SectionName>1</SectionName>
<PersonList>
<Person>
<Name>Ace</Name>
<Age>30</Age>
</Person>
<Person>
<Name>Zeus</Name>
<Age>2</Age>
</Person>
</PersonList>
</SectionList>
It took me about 6 hours to find and experiment. But not yet successful Please help.
Or something else, but must show PersonList too
This code i got But the result was not what I wanted
https://stackoverflow.com/a/6115782/8902883
public void SerializeObject<T>(T serializableObject, string fileName)
{
if (serializableObject == null) { return; }
try
{
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, serializableObject);
stream.Position = 0;
xmlDocument.Load(stream);
xmlDocument.Save(fileName);
}
}
catch (Exception ex)
{
//Log exception here
}
}
Output:
<?xml version="1.0"?>
<ArrayOfSectionList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SectionList>
<PersonList />
<SectionName>1</SectionName>
</SectionList>
<SectionList>
<PersonList />
<SectionName>2</SectionName>
</SectionList>
</ArrayOfSectionList>
//
Thank you so much, it's my mistake.
After I add a "Person" to the" SectionList"
SectionList.Add (Person)
Person.Clear();
"SectionName" remains, but all "Person" is missing.
Now I fixed it. Thank you so much.
you have made certain mistakes while implementing the code and I am assuming that you have used some xml librabry.
your code should be like below to bind with your models.
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="Person")]
public class Person
{
[XmlElement(ElementName="Name")]
public string Name { get; set; }
[XmlElement(ElementName="Age")]
public string Age { get; set; }
}
[XmlRoot(ElementName="PersonList")]
public class PersonList
{
[XmlElement(ElementName="Person")]
public List<Person> Person { get; set; }
}
[XmlRoot(ElementName="SectionList")]
public class SectionList
{
[XmlElement(ElementName="SectionName")]
public string SectionName { get; set; }
[XmlElement(ElementName="PersonList")]
public PersonList PersonList { get; set; }
}
}
Reference: https://xmltocsharp.azurewebsites.net/
Try following which eliminates one tag like your original posting:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
Root root = new Root() {
SectionList = new List<SectionList>() {
new SectionList()
{
SectionName = "1",
PersonList = new List<Person>() {
new Person() { Name = "Ace", Age = "30"}
}
}
}
};
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(Root));
serializer.Serialize(writer, root);
}
}
public class Person
{
public string Name{ get; set; }
public string Age{ get; set; }
}
public class Root
{
[XmlElement()]
public List<SectionList> SectionList { get; set; }
}
public class SectionList
{
[XmlElement("Person")]
public List<Person> PersonList { get; set; }
public string SectionName { get; set; }
}
}

How to Deserialize XMLElement with same name

I have XML root element and XML Element with same name, I am not sure how I should change my model class
The following code works as far as XML Element is not repeated with same name, in my case Gender list=1
Changing XML out format is not possible as it is coming from another system, unless filter out in C# code level
XML
<?xml version="1.0"?>
<Gender>
<Gender list="1">
<Item>
<CODE>M</CODE>
<DESCRIPTION>Male</DESCRIPTION>
</Item>
<Item>
<CODE>F</CODE>
<DESCRIPTION>Female</DESCRIPTION>
</Item>
</Gender>
</Gender>
Model Class
public class Gender
{
[XmlElement("Item")]
public List<Item> GenderList = new List<Item>();
}
public class Item
{
[XmlElement("CODE")]
public string Code { get; set; }
[XmlElement("DESCRIPTION")]
public string Description { get; set; }
}
XML Parsing Class
public static class XMLPrasing
{
public static Object ObjectToXML(string xml, Type objectType)
{
StringReader strReader = null;
XmlSerializer serializer = null;
XmlTextReader xmlReader = null;
Object obj = null;
try
{
strReader = new StringReader(xml);
serializer = new XmlSerializer(objectType);
xmlReader = new XmlTextReader(strReader);
obj = serializer.Deserialize(xmlReader);
}
catch (Exception exp)
{
//Handle Exception Code
var s = "d";
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
if (strReader != null)
{
strReader.Close();
}
}
return obj;
}
SECOND UPDATE
If I change my code with different Gender name as following then this work, question remain same how to handle with same name
<?xml version="1.0"?>
<Gender>
<GenderX list="1">
<Item>
<CODE>M</CODE>
<DESCRIPTION>Male</DESCRIPTION>
</Item>
<Item>
<CODE>F</CODE>
<DESCRIPTION>Female</DESCRIPTION>
</Item>
</GenderX>
</Gender>
Model class
[XmlRoot("Gender")]
public class Gender
{
[XmlElement("GenderX")]
public List<GenderX> GenderXList = new List<GenderX>();
}
public class GenderX
{
[XmlElement("Item")]
public List<Item> GenderList = new List<Item>();
}
public class Item
{
[XmlElement("CODE")]
public string Code { get; set; }
[XmlElement("DESCRIPTION")]
public string Description { get; set; }
}
I have found answer
[XmlRoot("Gender")]
public class Gender
{
[XmlElement("Gender")]
public List<GenderListWrap> _GenderListWrap = new List<GenderListWrap>();
}
public class GenderListWrap
{
[XmlAttribute("list")]
public string _ListTag { get; set; }
[XmlElement("Item")]
public List<Item> _GenderList = new List<Item>();
}
public class Item
{
[XmlElement("CODE")]
public string Code { get; set; }
[XmlElement("DESCRIPTION")]
public string Description { get; set; }
}
If you have only one top level Gender element, then this is enough:
[XmlRoot(ElementName = "Gender")]
public class Genders
{
[XmlElement(ElementName = "Gender")]
public Gender gender { get; set; }
}
public class Gender
{
[XmlElement(ElementName = "Item")]
public List<Item> GenderList = new List<Item>();
}
public class Item
{
[XmlElement("CODE")]
public string Code { get; set; }
[XmlElement("DESCRIPTION")]
public string Description { get; set; }
}
I'm using Generic method for deserialize XMLString.
This method take xml string and deserialized sender model Type.
You must use Model deserialized xml to property like this , and You should not forget to write [Serializable] for class attribute and [XmlElement] for property attribute
[Serializable]
public class Gender
{
[XmlElement("Item")]
public List<Item> GenderList = new List<Item>();
}
public class Item
{
[XmlElement("CODE")]
public string Code { get; set; }
[XmlElement("DESCRIPTION")]
public string Description { get; set; }
}
public static T Deserialize<T>(string input) where T : class
{
Log.Debug("Deserialize" + typeof(T).Name, "xml string Deserialize ediliyor" + Environment.NewLine + input);
XmlSerializer ser = new XmlSerializer(typeof(T), "SetDefaultNamespace"); // optinal parameters DefaultNamespace
using (StringReader sr = new StringReader(input))
{
var desearializedObject = (T)ser.Deserialize(sr);
Log.Debug("Deserialize" + typeof(T).Name, "Obje Deserialize işlemi tamamlandı");
return desearializedObject;
}
}
Deserialize<Gender>(xmlString);

Need help to deserialize XML data

I am trying to deserialize the below XML using C#
<?xml version="1.0" encoding="utf-8"?>
<Invoice>
<Samples>
<Sample>
<AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
<AccountNumber>55761598808</AccountNumber>
</Sample>
<Sample>
<AccountId>1e547ae6-9a6d-d18f-958b-22000b83a845</AccountId>
<AccountNumber>55761598808</AccountNumber>
</Sample>
</Samples>
</Invoice>
Here are the classes that I have defined to deserialize
[DataContract(Name = "Sample")]
public class Sample
{
[DataMember(Name = "AccountId")]
public string AccountId { get; set; }
[DataMember(Name = "AccountNumber")]
public string AccountNumber { get; set; }
}
[DataContract(Name = "Samples")]
public class Samples
{
[DataMember(Name = "Sample")]
public List<Sample> Sample { get; set; }
}
[DataContract(Name = "Invoice")]
public class Invoice
{
[DataMember(Name = "Samples")]
public Samples Samples { get; set; }
}
The corresponding test case to deserialize is as follows
public void SampleXmlTest()
{
dynamic env = SUT.GetEnvironment();
string dbConnStrUrjanet = (string)env.AvidUtility.UrjanetDB;
XmlSerializer deserializer = new XmlSerializer(typeof(CommonAvidXmlDto.Invoice));
TextReader reader = new StreamReader(#"C:\Users\SJuluru\Desktop\Sample XML\Samplexml.xml");
Object obj = deserializer.Deserialize(reader);
CommonAvidXmlDto.Invoice XmlData4 = (CommonAvidXmlDto.Invoice)obj;
After running the test case debug mode, XmlData4 has null without having the XML data. Kindly help me how to modify this code to work.
DataContract and DataMember are attribute of DataContractSerializer so you shoud replace your serializer msdn or use XmlSerializer's attributes (XmlRoot, XmlElement etc)
Change deserialize classes:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace ConsoleApp4
{
[XmlRoot(ElementName = "Sample")]
public class Sample
{
[XmlElement(ElementName = "AccountId")]
public string AccountId { get; set; }
[XmlElement(ElementName = "AccountNumber")]
public string AccountNumber { get; set; }
}
[XmlRoot(ElementName = "Samples")]
public class Samples
{
[XmlElement(ElementName = "Sample")]
public List<Sample> Sample { get; set; }
}
[XmlRoot(ElementName = "Invoice")]
public class Invoice
{
[XmlElement(ElementName = "Samples")]
public Samples Samples { get; set; }
}
}
Use below method for deserialize:
private static Invoice LoadInvoice(string fileName)
{
var serializer = new XmlSerializer(typeof(Invoice));
if (!File.Exists(fileName))
{
return null;
}
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return (Invoice)serializer.Deserialize(fs);
}
}
Result:

deserialize xml into inherited classes from base class

I have the following xml structure:
<Root1>
<name>Name1</name>
<company>Comp1</company>
<url>site.com</url>
<elements>
<element id="12" type="1">
<url>site1.com</url>
<price>15000</price>
...
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
</element>
<element id="13" type="2">
<url>site2.com</url>
<price>100</price>
...
<language>lg</language>
<binding>123</binding>
</element>
</elements>
</Root1>
I need to deserialize this xml into an object. You can see the element contains some equals field: url and price.
I would like to move these fields into a parent class and then inherit this class from other classes.
I created the class Root1:
namespace app1
{
[Serializable]
public class Root1
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("elements")]
public List<Element> ElementList { get; set; }
}
}
and then I created base class for Element:
[Serializable]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
}
and then I inherited this class from other classes:
[Serializable]
public class Element1 : Element
{
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
}
[Serializable]
public class Element2 : Element
{
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
When I deserialize this xml to object Root1 I get the object - it is ok.
But the List of Elements contains only Element objects not Element1 and Element2 objects.
How I do deserialize this xml so list of Elements contains Element1 and Element2 objects?
#netwer It is not working for you because the code suggested above generates the xml (below) that does not match with the one you use for deserialization (see how it specifies derived types in for element).
<?xml version="1.0" encoding="utf-8"?>
<Root1>
<name>Name1</name>
<company>Comp1</company>
<url>site.com</url>
<elements>
<element d3p1:type="Element1" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site1.com</url>
<price>15000</price>
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
</element>
<element d3p1:type="Element2" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site2.com</url>
<price>100</price>
<language>lg</language>
<binding>123</binding>
</element>
</elements>
</Root1>
So you will either have to match this format with source xml (change code or API that returns this xml data) or take another approach. Even if you manage to do with former one you have to find a way to access Element1 or Element2 specific properties.
newRoot1.Elements.ElementList[i] will always let you access only price and url since your list is of Element type. Although run time type of ElementList[i] will be Element1 or Element2, how will you detect that?
Here I suggest alternative approach. Irrespective of whether your application (client) generates this xml or the server which returns it on hitting API, you should be able to gather information on all the fields applicable to an 'element' object. If it is your code you know it already, if it is an API there must be a document for it. This way you only have to create one 'Element' (no derived classes) and put proper checks (mostly string.IsNullOrEmpty()) before accessing Element class property values in your code. Only the properties that are present in your xml 'element' element will be considered rest will be set to NULL for that instance.
[Serializable]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
I think you should use XmlIncludeAttribute like this:
[XmlInclude(typeof(Element1))]
public class Element
{
}
Here is xml and code. I like to first serialize with test data, then deserialize.
<?xml version="1.0" encoding="utf-8"?>
<Root1>
<name>Name1</name>
<company>Comp1</company>
<url>site.com</url>
<element d2p1:type="Element1" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site1.com</url>
<price>15000</price>
<manufacturer_warranty>true</manufacturer_warranty>
<country_of_origin>Япония</country_of_origin>
</element>
<element d2p1:type="Element2" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
<url>site2.com</url>
<price>100</price>
<language>lg</language>
<binding>123</binding>
</element>
</Root1>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string FILENAME = #"c:\temp\test.xml";
Root1 root1 = new Root1() {
Name = "Name1",
Company = "Comp1",
Url = "site.com",
ElementList = new List<Element>() {
new Element1() {
Url = "site1.com",
Price = "15000",
mw = "true",
co = "Япония"
},
new Element2() {
Url = "site2.com",
Price = "100",
lg = "lg",
bind = "123"
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Root1));
StreamWriter writer = new StreamWriter(FILENAME);
XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
_ns.Add("", "");
serializer.Serialize(writer, root1, _ns);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Root1));
XmlTextReader reader = new XmlTextReader(FILENAME);
Root1 newRoot1 = (Root1)xs.Deserialize(reader);
}
}
[XmlRoot("Root1")]
public class Root1
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("element")]
public List<Element> ElementList { get; set; }
}
[XmlInclude(typeof(Element1))]
[XmlInclude(typeof(Element2))]
[XmlRoot("element")]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
}
[XmlRoot("element1")]
public class Element1 : Element
{
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
}
[XmlRoot("element2")]
public class Element2 : Element
{
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
}
Code below matches better with your posted XML. You need to compare the generated xml with your xml.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string FILENAME = #"c:\temp\test.xml";
Root1 root1 = new Root1()
{
Name = "Name1",
Company = "Comp1",
Url = "site.com",
cElement = new Elements() {
ElementList = new List<Element>() {
new Element1() {
Url = "site1.com",
Price = "15000",
mw = "true",
co = "Япония"
},
new Element2() {
Url = "site2.com",
Price = "100",
lg = "lg",
bind = "123"
}
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Root1));
StreamWriter writer = new StreamWriter(FILENAME);
XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
_ns.Add("", "");
serializer.Serialize(writer, root1, _ns);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Root1));
XmlTextReader reader = new XmlTextReader(FILENAME);
Root1 newRoot1 = (Root1)xs.Deserialize(reader);
}
}
[XmlRoot("Root1")]
public class Root1
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("elements")]
public Elements cElement { get; set; }
}
[XmlRoot("elements")]
public class Elements
{
[XmlElement("element")]
public List<Element> ElementList { get; set; }
}
[XmlInclude(typeof(Element1))]
[XmlInclude(typeof(Element2))]
[XmlRoot("element", Namespace = "")]
public class Element
{
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("price")]
public string Price { get; set; }
}
[XmlRoot("element1", Namespace = "")]
public class Element1 : Element
{
[XmlElement("manufacturer_warranty")]
public string mw { get; set; }
[XmlElement("country_of_origin")]
public string co { get; set; }
}
[XmlRoot("element2", Namespace = "")]
public class Element2 : Element
{
[XmlElement("language")]
public string lg { get; set; }
[XmlElement("binding")]
public string bind { get; set; }
}
}​

How to serialize selected properties only

I have one class Person and it has some public properties . Now I want to serialize person class but with some selected properties only. This can be achieve by making that property private but I don't want to change any property as private.
If its not possible using serialisation then, what is another way to create xml doc for object with selected properties only.
Note: All properties must be public.
class Program
{
static void Main(string[] args)
{
Person person = new Person();
using (MemoryStream memoryStream = new MemoryStream())
{
XmlSerializer xmlSerializer = new XmlSerializer(person.GetType());
xmlSerializer.Serialize(memoryStream, person);
using (FileStream fileStream = File.Create("C://Output.xml"))
{
memoryStream.WriteTo(fileStream);
fileStream.Close();
}
memoryStream.Close();
}
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public double Salary { get; set; }
public Person()
{
Id = 1;
Name = "Sam";
Salary = 50000.00;
}
}
Current Output
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>1</Id>
<Name>Sam</Name>
<Salary>50000</Salary>
</Person>
Expected Output
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Salary>50000</Salary>
</Person>
You could use an XmlIgnore attribute..
public class Person
{
[XmlIgnore]
public int Id { get; set; }
[XmlIgnore]
public string Name { get; set; }
public double Salary { get; set; }
public Person()
{
Id = 1;
Name = "Sam";
Salary = 50000.00;
}
}
See this
can you give a try to below code and just use XmlIgnore attribute on he properties which you not require :-
[XmlIgnore]
public int Id { get; set; }
[XmlIgnore]
public string Name { get; set; }
public double Salary { get; set; }

Categories