Writing Xml Class in c# - c#

I have a xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Devices>
<Settings>
<Name>ABC</DeviceName>
<HostNic>LAN_1</HostNic>
</Settings>
</Devices>
</Configuration>
I want to deserialize this to object form. I am trying to define a class structure for the xml file like this:
class Configuration
{
[XmlElement("Address")]
public List<Devices> deviceList = new List<Devices>();
}
class Devices
{
[XmlElement("Address")]
public List<Settings> settingList = new List<Settings>();
}
class Settings
{
public string Name { get; set; }
public string HostNic { get; set; }
}
Is there any other appropriate way of defining class for this xml file?

Your Classes need some modifications, specially the attributes you have added.
[XmlRoot]
public class Configuration
{
[XmlElement("Devices")]
public List<Devices> deviceList = new List<Devices>();
}
public class Devices
{
[XmlElement("Settings")]
public List<Settings> settingList = new List<Settings>();
}
public class Settings
{
public string Name { get; set; }
public string HostNic { get; set; }
}
Then you can de-serialize the XML into the above classes:
var serializer = new XmlSerializer(typeof(Configuration));
using (System.IO.TextReader reader = new System.IO.StringReader(<Your XML String>))
{
Configuration config = (Configuration)serializer.Deserialize(reader);
}

Related

Setting a XML Element Name and XML Attribute to a Class Property

I have this Class that I want to convert to an XML Element
public class Person
{
[XmlElement(ElementName = "PersonName")]
public string Name { get; set; }
}
This will display an XML
<Person>
<PersonName>Smith</PersonName>
</Person>
I want to add an attribute to the element PersonName
<Person>
<PersonName required="true">Smith</PersonName>
</Person>
How would I do that?
I think you need a special class to hold your Name property, rather than relying on string. Here is an example, using the XmlText and XmlAttribute attributes to control how the built-in XmlSerializer works:
using System.Xml.Serialization;
using System.IO;
namespace SomeNamespace
{
class Program
{
static void Main(string[] args)
{
Person me = new Person("me");
string path = "C:\\temp\\person.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (StreamWriter sw = new StreamWriter(path))
{
serializer.Serialize(sw, me);
}
}
}
public class Person
{
public Person() { } // needed for serialization
public Person(string name)
{
Name = new PersonName(name);
}
[XmlElement(ElementName = "PersonName")]
public PersonName Name { get; set; }
}
public class PersonName
{
public PersonName() { } // needed for serialization
public PersonName(string name)
{
Name = name;
}
[XmlText]
public string Name { get; set; }
[XmlAttribute] // serializes as an Attribute
public bool Required { get; set; } = true;
}
}
output (at C:\temp\person.xml; you can change Main to serialize to string and print to console if you want):
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PersonName Required="true">me</PersonName>
</Person>
If you really want your Required attribute to be serialized as the lowercase "required", you can use different properties of XmlAttribute, such as: XmlAttribute(AttributeName = "required")

Parse nested XML from URL

I'm currently working on a .NET 4.6 console application. I need to parse a nested XML from an URL and transform the XML into an object list.
The URL for the sample XML is the following:
https://www.w3schools.com/xml/cd_catalog.xml
The XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
My corresponding C# classes look like this:
[XmlRoot(ElementName = "CATALOG")]
public class Catalog
{
[XmlElement("CD")]
List<Cd> Cds { get; set; }
}
[XmlRoot(ElementName = "CD")]
public class Cd
{
[XmlElement("TITLE")]
public string Title { get; set; }
[XmlElement("ARTIST")]
public string Artist { get; set; }
[XmlElement("COUNTRY")]
public string Country { get; set; }
[XmlElement("COMPANY")]
public string Company { get; set; }
[XmlElement("PRICE")]
public double Price { get; set; }
[XmlElement("YEAR")]
public int Year { get; set; }
}
My program class looks like this:
class Program
{
static void Main(string[] args)
{
Init();
}
public static void Init() {
var url = "https://www.w3schools.com/xml/cd_catalog.xml";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(url);
var catalog = myXmlDocument.InnerXml.ToString();
var result = Deserialize<Catalog>(catalog);
// result is null :(
Console.ReadKey();
}
public static T Deserialize<T>(string xmlText)
{
try
{
var stringReader = new StringReader(xmlText);
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(stringReader);
}
catch (Exception ex)
{
throw;
}
}
}
So far so good, my variable catalog consists out of an XML string, but somehow the XML doesn't get parsed correctly. I always get null as a return result. Perhaps it's because of my class definitions.
What do you think, do you have an idea on how to solve this issue? To retrieve an List<Cd> instead of null.
The error is very subtle. You have done everything right, but you missed to add the public access qualifier in your Catalog class, on the list of Cd like so:
[XmlRoot(ElementName = "CATALOG")]
public class Catalog
{
[XmlElement("CD")]
public List<Cd> Cds { get; set; }
}
Since the access qualifiers default to private, the deserializer is having a hard time finding the correct property to deserialize the XML into.
List<Cd> Cds { get; set; }
Change this line to
Public List<Cd> Cds { get; set; }

Creating XML structure using xml attributes

I am trying to solve a puzzle with xml attributes. The problem is that we already have widely used file with this structure, from which I can't deviate
<CONFIGS>
<CONFIG>
<NAME>c1</NAME>
<DB>
<VAL1>v1</VAL1>
<VAL2>v2</VAL2>
<VAL3>v3</VAL3>
</DB>
</CONFIG>
<CONFIG>
<NAME>c2</NAME>
<DB>
<VAL1>v1</VAL1>
<VAL2>v2</VAL2>
<VAL3>v3</VAL3>
</DB>
</CONFIG>
</CONFIGS>
I've created this c# code
// master class
[XmlRoot(ElementName = "CONFIGS")]
public class MyConfigs
{
[XmlArrayItem(ElementName = "CONFIG", Type = typeof(MyConfigSchema))]
public MyConfigSchema[] Schemas { get; set; }
}
// I should have array of these
public class MyConfigSchema
{
[XmlElement(DataType = "string", ElementName = "NAME")]
public string Name { get; set; }
[XmlElement(ElementName = "DB", Type = typeof(Db))]
public Db Config { get; set; }
// this element is single and has subelements
public class Db
{
[XmlElement(DataType = "string", ElementName = "VAL1")]
public string Val1 { get; set; }
[XmlElement(DataType = "int", ElementName = "VAL2")]
public int Val2 { get; set; }
[XmlElement(DataType = "string", ElementName = "VAL3")]
public string Val3 { get; set; }
}
}
// Writing
using (var writer = new FileStream(testfile, FileMode.Create))
{
var ser = new XmlSerializer(typeof(MyConfigs));
ser.Serialize(writer, confFileObj);
writer.Close();
}
Here is my issue - it writes the following output, which is almost what I need but in there it writes <Schemas>. . . </Schemas> that I can't have.
<CONFIGS>
--<Schemas>--
<CONFIG>
<NAME>c1</NAME>
<DB>
<VAL1>v1</VAL1>
<VAL2>v2</VAL2>
<VAL3>v3</VAL3>
</DB>
</CONFIG>
<CONFIG>
<NAME>c2</NAME>
<DB>
<VAL1>v1</VAL1>
<VAL2>v2</VAL2>
<VAL3>v3</VAL3>
</DB>
</CONFIG>
--</Schemas>--
</CONFIGS>
Is there a way of get rid of <Schemas>. . . </Schemas>?
Looks like I just solved it. I have never seen this before, looked on MSDN, hence I didn't try it. But I tried instead of this
[XmlArrayItem(ElementName = "CONFIG", Type = typeof(MyConfigSchema))]
public MyConfigSchema[] Schemas { get; set; }
do this
[XmlElement(ElementName = "CONFIG", Type = typeof(MyConfigSchema))]
public MyConfigSchema[] Schemas { get; set; }
Instead of XmlArrayItem I placed XmlElement and it worked. I didn't know one can mark List or array with plain element attribute.

C# Deserialization xml file

I try to deserialize xml file:
<?xml version="1.0" encoding="utf-8"?>
<XmlFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OBJECTS ITEM="ItemValue" TABLE_NAME="TableExample">
</OBJECTS>
</XmlFile>
My deserialize class code looks like that:
[Serializable]
[XmlRoot("XmlFile")]
public class SerializeObject
{
[XmlAttribute("ITEM")]
public string Item { get; set; }
[XmlAttribute("TABLE_NAME")]
public string Table_Name { get; set; }
}
When I try deserialize xml file i always got no errors and Item and Table_Name equals null. Why?
Thx for replay
[XmlRoot("XmlFile")]
public class SerializableContainer
{
[XmlElement("OBJECTS")]
public SerializeObject[] Objects { get; set; }
}
public class SerializeObject
{
[XmlAttribute("ITEM")]
public string Item { get; set; }
[XmlAttribute("TABLE_NAME")]
public string Table_Name { get; set; }
}
And then you deserialize with:
var serializer = new XmlSerializer(typeof(SerializableContainer));
using (var file = File.OpenText("sample.xml"))
{
var data = (SerializableContainer)serializer.Deserialize(file);
// ...
}
leaving here a more complete example in case anyone needs: http://davidsonsousa.net/en/post/serializedeserialize-objects-to-xml-with-c
Cheers!

Deserialize XML to Object Array

I'm trying to deserialize an XML file to an object array, but I'm receiving empty objects.
My question looks similar to this: How to Deserialize xml to an array of objects? but I can't seem to create a class which inherits IXmlSerializable. That said, I don't think that approach is necessary.
Am I doing something wrong?
File Object
[XmlType("file")]
public class File
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("company_name")]
public string Company_Name { get; set; }
[XmlElement("docs")]
public HashSet<doc> Docs { get; set; }
}
Doc Object
[XmlType("doc")]
public class Doc
{
[XmlElement("valA")]
public string ValA { get; set; }
[XmlElement("valB")]
public string ValB { get; set; }
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<files>
<file>
<id>12345</id>
<company_name>Apple</company_name>
<docs>
<doc>
<valA>Info</valA>
<valB>More Info</valB>
</doc>
</docs>
</file>
<file>
<id>12345</id>
<company_name>Microsoft</company_name>
<docs>
<doc>
<valA>Even More Info</valA>
<valB>Lots of it</valB>
</doc>
</docs>
</file>
</files>
Deserialization code
XmlSerializer mySerializer = new XmlSerializer(typeof(File[]), new XmlRootAttribute("files"));
using (FileStream myFileStream = new FileStream("Files.xml", FileMode.Open))
{
File[] r;
r = (File[])mySerializer.Deserialize(myFileStream);
}
You have decorated your properties with XMLAttribute but they are elements in your XML. So, change all XMLAttribute to XmlElement.
[XmlType("file")]
public class File
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("company_name")]
public string Company_Id { get; set; }
[XmlArray("docs")]
public HashSet<Doc> Docs { get; set; }
}
[XmlType("doc")]
public class Doc
{
[XmlElement("valA")]
public string ValA { get; set; }
[XmlElement("valB")]
public string ValB { get; set; }
}
Also you XML is not well formed. I guess this is typo though -
<company_name>Apple</company_id>
<company_name>Microsoft</company_id>
Ending tag should be company_name -
<company_name>Apple</company_name>
<company_name>Microsoft</company_name>
I would use xml parser..
XDocument doc=XDocument.Load(url);
File[] r=doc.Elements("file")
.Select(f=>
new File
{
Id=f.Element("id").Value,
Company_Id=f.Element("company_name").Value,
Docs=new HashSet<Docs>(
f.Elements("docs")
.Elements("doc")
.Select(d=>
new Doc
{
ValA=d.Element("valA").Value,
ValB=d.Element("valB").Value
}))
}).ToArray();

Categories