Problem creating XML using c# - c#

I am searching a batter solution for creating xml through xml serialization. What i need, i have a given format like this
<product Id="1">
<name>2 1/2 X 6 PVC NIPPLE TOE SCH 80</name>
<notes>
<note>!--note 1---</note>
<note>!--note 2--</note>
......
</notes>
</product>
what i am doing here, i created a 2 classes like this
public class product
{
[XmlElement("name")]
public string Name { get; set; }
[XmlArray("notes")]
public List<notes> ListNotes { get; set; }
}
public class notes
{
[XmlIgnore]
public string Note { get; set; }
}
when i am serializing this then i am getting xml in this formate
<product Id="1">
<name>2 1/2 X 6 PVC NIPPLE TOE SCH 80</name>
<notes>
<notes>
<note>!--note 1---</note>
<note>!--note 2--</note>
</notes>
</notes>
</product>
i don't want extra <notes>. Any batter solution to solve this problem?
Thanks
Solution
public class product
{
[XmlElement("name")]
public string Name { get; set; }
[XmlArray("notes")]
public List<notes> ListNotes { get; set; }
}
public class notes
{
[XmlText]
public string Note { get; set; }
}
product ObjProduct = new product
{
Name ="Pankaj",
notes=new List<note>()
}
foreach (var note in item.ListNote)
{
ObjProduct.notes.Add(new Highmark.BLL.Common.note { Note = EncodeTo64(note.Note) });
}
Now use this ObjProduct for serialization.

Try like this:
[XmlRoot("product")]
public class Product
{
[XmlAttribute]
public int Id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("note")]
public List<Note> ListNotes { get; set; }
}
public class Note
{
[XmlText]
public string Text { get; set; }
}
class Program
{
public static void Main()
{
var p = new Product
{
Id = 1,
Name = "2 1/2 X 6 PVC NIPPLE TOE SCH 80",
ListNotes = new List<Note>
{
new Note { Text = "!--note 1---" },
new Note { Text = "!--note 2---" },
}
};
var serializer = new XmlSerializer(p.GetType());
serializer.Serialize(Console.Out, p);
}
}
And if you want to remove the namespace from the root node:
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(Console.Out, p, ns);

Try like this:
class Program
{
static void Main(string[] args)
{
var product = new Product() { Name = "PVC SCHOOL" };
product.Notes = new List<note>();
product.Notes.Add(new note() { Note = "A" });
product.Notes.Add(new note() { Note = "B" });
var serialer = new XmlSerializer(typeof(Product));
using (var stream = new StreamWriter("test.txt"))
{
serialer.Serialize(stream, product);
}
}
}
public class Product
{
[XmlElement("name")]
public string Name { get; set; }
[XmlArray("notes")]
public List<note> Notes { get; set; }
}
public class note
{
[XmlIgnore]
public string Note { get; set; }
}

This doesn't directly answer your question, but if you can't figure out this problem, you can create a Schema.xsd file, and use .NET's XSD tool to generate the proper serialization classes for you.
I've had pretty good success with that.
The obvious benefit of using a schema is the validation on XML, prior to serialization.

Related

C# XML reading foreach always first value

My XML
<?xml version="1.0" encoding="UTF-8"?>
<teklif>
<bilgiler>
<firma>Firma Adı</firma>
<aciklama>Açıklama</aciklama>
<isim>Ad Soyad</isim>
<telefon>Telefon</telefon>
<eposta>E Posta</eposta>
<urunler>
<urun>
<resimDosyasi>Dosya Seçilmedi</resimDosyasi>
<aciklama>Ürün Açıklaması</aciklama>
<birim>3,00</birim>
<miktar>1</miktar>
<toplam>0,00</toplam>
</urun>
<urun>
<resimDosyasi>Dosya Seçilmedi</resimDosyasi>
<aciklama>Ürün Açıklaması</aciklama>
<birim>5,00</birim>
<miktar>1</miktar>
<toplam>0,00</toplam>
</urun>
<urun>
<resimDosyasi>Dosya Seçilmedi</resimDosyasi>
<aciklama>aas</aciklama>
<birim>2,00</birim>
<miktar>1</miktar>
<toplam>0,00</toplam>
</urun>
</urunler>
And My Function which reads the XML file
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNodeList xmllist = doc.SelectNodes("/teklif/bilgiler/urunler");
foreach(XmlNode nod in xmllist)
{
foreach(XmlNode childNode in nod.ChildNodes)
{
// her ürünün childnode oldu
if(childNode.Name == "#text")
{
} else
{
var urun_resim = childNode.SelectSingleNode("//resimDosyasi").InnerText;
var urun_aciklama = childNode.SelectSingleNode("//aciklama").InnerText;
var urun_birim = childNode.SelectSingleNode("//birim").InnerText;
MessageBox.Show(urun_birim);
var urun_miktar = childNode.SelectSingleNode("//miktar").InnerText;
var urun_toplam = childNode.SelectSingleNode("//toplam").InnerText;
var urun = new Urun(urun_resim, urun_birim, urun_miktar, urun_aciklama);
lw_urunler.Items.Add(urun);
}
}
}
The problem is when I message box the //birim in foreach loop, it always writes the first one - 3,00((3 times). As you can see in XML, first is 3,00, the second is 5,00 and the third is 2,00 but it always writes the first one. I checked up a lot but I can't see the problem.
Try it without the //, e.g. childNode.SelectSingleNode("birim"). Two forward slashes mean the root of the XML document, and my guess is it's just always finding the first birim node starting from the root each time.
The // means to select nodes no matter where they are under the current context. Current context defaults to the root. To limit the results to those under the current child node follow this pattern (just add a dot):
var urun_resim = childNode.SelectSingleNode(".//resimDosyasi").InnerText;
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication52
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<urun> uruns = doc.Descendants("urun").Select(x => new urun() {
resimDosyasi = (string)x.Element("resimDosyasi"),
aciklama = (string)x.Element("aciklama"),
birim = (string)x.Element("birim"),
miktar = (int)x.Element("miktar"),
toplam = (string)x.Element("toplam")
}).ToList();
}
}
public class urun
{
public string resimDosyasi { get; set; }
public string aciklama { get; set; }
public string birim { get; set; }
public int miktar { get; set; }
public string toplam { get; set; }
}
}
Another approach that would increase the readability and maintenance of your code would be to map the XML schema into an object model.
Doing so, you could easly load the XML data as follows:
XmlSerializer serializer = new XmlSerializer(typeof(Teklif));
using (FileStream fileStream = new FileStream(filename, FileMode.Open))
{
Teklif result = (Teklif)serializer.Deserialize(fileStream);
}
// Object model example
[XmlRoot("teklif")]
public class Teklif
{
[XmlElement()]
public bilgiler bilgiler { get; set; }
}
public class bilgiler
{
[XmlElement()] public string firma { get; set; }
[XmlElement()] public string aciklama { get; set; }
[XmlElement()] public string isim { get; set; }
[XmlElement()] public string telefon { get; set; }
[XmlElement()] public string eposta { get; set; }
[XmlArray()]
[XmlArrayItem("urun")]
public List<Urun> urunler { get; set; }
}
public class Urun
{
[XmlElement()] public string resimDosyasi { get; set; }
[XmlElement()] public string aciklama { get; set; }
[XmlElement()] public string birim { get; set; }
[XmlElement()] public int miktar { get; set; }
[XmlElement()] public string toplam { 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);

Read XML file parse it into a class list

This is the code that I've used to write the data from the List to the XML file:
using (FileStream f = new FileStream(#"animals.xml", FileMode.Create, FileAccess.Write))
{
var dcsw = new DataContractSerializer(typeof(List<Animal>));
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(f);
try
{
dcsw.WriteObject(f, animalList);
writer.Close();
f.Close();
return true;
}
catch (Exception)
{
return false;
}
This is an example of my XML file:
<ArrayOfAnimal xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AnimalShelter">
<Animal i:type="Cat">
<ChipRegistrationNumber>12346</ChipRegistrationNumber>
<DateOfBirth>
<date>1234-11-11T00:00:00</date>
</DateOfBirth>
<IsReserved>false</IsReserved>
<Name>Yoshi</Name>
<BadHabits>Scratches</BadHabits>
<Price>51</Price>
</Animal>
<Animal i:type="Dog">
<ChipRegistrationNumber>12347</ChipRegistrationNumber>
<DateOfBirth>
<date>1234-11-11T00:00:00</date>
</DateOfBirth>
<IsReserved>false</IsReserved>
<Name>Sonic</Name>
<LastWalkDate>
<date>1234-11-11T00:00:00</date>
</LastWalkDate>
<Price>200</Price>
</Animal>
</ArrayOfAnimal>
As you can see, Cat has a a node called "BadHabits" and Dog has a different one called "LastWalkDate".
"Cat" and "Dog" inherit from the class Animal.
I've seen many examples of parsing strings to a List.. That's quite do-able.. but my list has different types like a custom date class
Example of an instance of Animal:
SimpleDate date1 = new SimpleDate(11, 11, 1234);
Animal cat1 = new Cat("12346", date1, "Yoshi", "Scratches");
How am I able to parse all the cats and dogs from the XML file in a List<Animal>?
You can probably use something like this:
using (FileStream f = new FileStream(<pathtoxml>, FileMode.Open, FileAccess.Read))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(List<Animal>));
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(f, new XmlDictionaryReaderQuotas());
List<Animal> listfromxml = (List<Animal>)dcs.ReadObject(reader);
}
Here is your class related to your XML file.
Generated with this tools
EDIT : you have an invalid XML near from the "Price" element : <Price>51</rice>
[XmlRoot(ElementName="DateOfBirth", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public class DateOfBirth {
[XmlElement(ElementName="date", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string Date { get; set; }
}
[XmlRoot(ElementName="Animal", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public class Animal {
[XmlElement(ElementName="ChipRegistrationNumber", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string ChipRegistrationNumber { get; set; }
[XmlElement(ElementName="DateOfBirth", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public DateOfBirth DateOfBirth { get; set; }
[XmlElement(ElementName="IsReserved", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string IsReserved { get; set; }
[XmlElement(ElementName="Name", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string Name { get; set; }
[XmlElement(ElementName="BadHabits", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string BadHabits { get; set; }
[XmlElement(ElementName="Price", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string Price { get; set; }
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlElement(ElementName="LastWalkDate", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public LastWalkDate LastWalkDate { get; set; }
}
[XmlRoot(ElementName="LastWalkDate", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public class LastWalkDate {
[XmlElement(ElementName="date", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public string Date { get; set; }
}
[XmlRoot(ElementName="ArrayOfAnimal", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public class ArrayOfAnimal {
[XmlElement(ElementName="Animal", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
public List<Animal> Animal { get; set; }
[XmlAttribute(AttributeName="i", Namespace="http://www.w3.org/2000/xmlns/")]
public string I { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
}

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; }
}
}​

XML serialization array in C#

I'm having trouble figuring this out, I have an xml sheet that looks like this
<root>
<list id="1" title="One">
<word>TEST1</word>
<word>TEST2</word>
<word>TEST3</word>
<word>TEST4</word>
<word>TEST5</word>
<word>TEST6</word>
</list>
<list id="2" title="Two">
<word>TEST1</word>
<word>TEST2</word>
<word>TEST3</word>
<word>TEST4</word>
<word>TEST5</word>
<word>TEST6</word>
</list>
</root>
And I'm trying to serialize it into
public class Items
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlAttribute("title")]
public string Title { get; set; }
//I don't know what to do for this
[Xml... something]
public list<string> Words { get; set; }
}
//I don't this this is right either
[XmlRoot("root")]
public class Lists
{
[XmlArray("list")]
[XmlArrayItem("word")]
public List<Items> Get { get; set; }
}
//Deserialize XML to Lists Class
using (Stream s = File.OpenRead("myfile.xml"))
{
Lists myLists = (Lists) new XmlSerializer(typeof (Lists)).Deserialize(s);
}
I'm really new with XML and XML serializing, any help would be much appreciated
It should work if you declare your classes as
public class Items
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlAttribute("title")]
public string Title { get; set; }
[XmlElement("word")]
public List<string> Words { get; set; }
}
[XmlRoot("root")]
public class Lists
{
[XmlElement("list")]
public List<Items> Get { get; set; }
}
If you just need to read your XML into an object structure, it might be easier to use XLINQ.
Define your class like so:
public class WordList
{
public string ID { get; set; }
public string Title { get; set; }
public List<string> Words { get; set; }
}
And then read the XML:
XDocument xDocument = XDocument.Load("myfile.xml");
List<WordList> wordLists =
(
from listElement in xDocument.Root.Elements("list")
select new WordList
{
ID = listElement.Attribute("id").Value,
Title = listElement.Attribute("title").Value,
Words =
(
from wordElement in listElement.Elements("word")
select wordElement.Value
).ToList()
}
).ToList();

Categories