I'm trying to deserialize simple xml file:
<thesaurus xmlns="http://marklogic.com/xdmp/thesaurus">
<metadata>
</metadata>
<entry>
<term>a</term>
<synonym>
<term>as</term>
</synonym>
</entry>
<entry>
<term>b</term>
<synonym>
<term>bs</term>
</synonym>
<synonym>
<term>bss</term>
</synonym>
</entry>
</thesaurus>
I'm using XmlSerializer like this:
var xmlSerializer = new XmlSerializer(typeof(Thesaurus));
var thesaurus = xmlSerializer.Deserialize(stream);
My model looks like this:
[Serializable]
[XmlRoot("thesaurus", Namespace = "http://marklogic.com/xdmp/thesaurus")]
public class Thesaurus
{
[XmlElement("metadata")]
public Metadata Metadata { get; set; }
[XmlElement("entry")]
public List<Entry> Entries { get; set; }
}
public class Metadata
{
}
public class Entry
{
[XmlElement("term")]
public string Term { get; set; }
[XmlElement("synonym")]
public String[] Synonym { get; set; }
}
So when I'm running this code, I get deserialized thesaurus object with parsed metadata and 1 entry with filled term and synonym fields. I can't get all of the entries here.
BUT
when I comment out Synonym field it starts giving me 2 entries in thesaurus object. I can't wrap entries in <entries> tag because it's some internal format of an application I'm feeding with this xml file.
Anyone has any ideas how to parse this xml file correctly? I tried searching for a solution, but this xml looks quite different than ones in examples.
Ok, so if you need to keep inside synonim field array of terms fields you need to change your Entry class to something like this:
public class Entry
{
[XmlElement("term")]
public string Term { get; set; }
[XmlElement("synonim")]
public Term[] Synonym { get; set; }
}
also you'll need to add additional one:
public class Term
{
[XmlElement("term")]
public string Value { get; set; }
}
This way you'll have what you need.
So, additional hierarchy level was added by additional class.
Please find below code for your test:
var xmlSerializer = new XmlSerializer(typeof(Thesaurus));
var r = new Thesaurus();
r.Entries = new List<Entry>();
r.Metadata = new Metadata();
r.Entries.Add(new Entry()
{
Synonym = new Term[] { new Term(){Value = "1"}, new Term() {Value = "2"}, },
Term = "Term1"
});
r.Entries.Add(new Entry()
{
Synonym = new Term[] { new Term() { Value = "3" }, new Term() { Value = "4" }, },
Term = "Term2"
});
using (TextWriter writer = new StreamWriter(#"c:\111.xml"))
{
xmlSerializer.Serialize(writer, r);
writer.Close();
}
using (TextReader reader = new StreamReader(#"c:\111.xml"))
{
Thesaurus tt = xmlSerializer.Deserialize(reader) as Thesaurus;
Console.Write(tt.Entries.Count);
reader.Close();
}
Related
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Images>
<I0>
<Path>123.com</Path>
<I0>
<I1>
<Path>123.com</Path>
<I1>
<I2>
<Path>123.com</Path>
<I2>
</Images>
Can serializer.Deserialize() be used to get tags with different names into a collection?
currently, in my object I have:
C#:
public class rootObject
{
[XmlElement(ElementName = "I0")]
public I0 I0 { get; set; }
[XmlElement(ElementName = "I1")]
public I1 I1 { get; set; }
[XmlElement(ElementName = "I2")]
public I2 I2 { get; set; }
}
But I would like to have (Because Images can have more or fewer elements):
public class rootObject
{
public List<I> Is { get; set; }
}
You can do what you are suggesting you just merely need to pass in the type argument in your class doing the generic. The key point to remember when you do a deserialization routine is that the routine needs to know the sub reference. So if I was to say string.Deserialize it would bomb. It would need to know a reference string.Deserialize> where Sub could be the class object that may change.
Say I have a base class and I want 'T' to be a type I can change for extensible abilities later.
[Serializable]
public class Test<T> where T : class
{
public Test() { }
public int TestId { get; set; }
public string Name { get; set; }
public List<T> Shipments { get; set; }
}
I want to test this with two classes I just make up that have different properties slightly
[Serializable]
public class Sub1
{
public int Id { get; set; }
public string Desc { get; set; }
}
[Serializable]
public class Sub2
{
public int IdWhatever { get; set; }
public string DescWhatever { get; set; }
}
Now let's do a main program and test serialization.
class Program
{
static void Main(string[] args)
{
var serializeTest = new Test<Sub1> { TestId = 1, Name = "Test", Shipments = new List<Sub1> { new Sub1 { Id = 1, Desc = "Test" }, new Sub1 { Id = 2, Desc = "Test2" } } };
var serializeTest2 = new Test<Sub2> { TestId = 1, Name = "Test", Shipments = new List<Sub2> { new Sub2 { IdWhatever = 1, DescWhatever = "Test" }, new Sub2 { IdWhatever = 2, DescWhatever = "Test2" } } };
var serialized = serializeTest.SerializeToXml();
var serialized2 = serializeTest2.SerializeToXml();
var deserialized = serialized.DeserializeXml<Test<Sub1>>();
var deserialized2 = serialized2.DeserializeXml<Test<Sub2>>();
Console.WriteLine(serialized);
Console.WriteLine();
Console.WriteLine(serialized2);
Console.ReadLine();
}
}
And my Serialize and DeSerialize extension methods:
public static string SerializeToXml<T>(this T valueToSerialize, string namespaceUsed = null)
{
var ns = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, (namespaceUsed != null) ? namespaceUsed : string.Empty) });
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
dynamic xmler = new XmlSerializer(typeof(T));
xmler.Serialize(writer, valueToSerialize, ns);
}
return sw.ToString();
}
}
public static T DeserializeXml<T>(this string xmlToDeserialize)
{
dynamic serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xmlToDeserialize))
{
return (T)serializer.Deserialize(reader);
}
}
You don't need to specify the XmlElement name when the properties match the XML. A few solutions, some kinda hacky :).
HACKY: use regex string replace to replace <I#> and </I#> to
just <I> and </I>
SOMEWHAT HACKY: This might work for you:
How to deserialize an XML array containing multiple types of elements in C#,
but you'd have to add an attribute for i0, i1 ... i100, etc.
BEST: Is that your entire XML? I'd honestly just use LINQToXml and
do a Descendants("Path") and get an array of strings back with 1 line of code. Serialization is not really the best solution for this.
I am trying to convert a html list to xml format with a console application, but i did what i planned and now i dont know how to continue. I will share my code and explain a bit. What i dont know for now , and is confusing me is where the 'magic' happens. Ok i know i have to take that list from the page , read the list with all the tags inside, but what next, how can i transform that list into xml format? I am new to xml i know some basics so please help me.
Here is the application :
static void Main(string[] args)
{
string _url = "http://example.com/media";
int newsCounter = 0;
List<News> _newsList = new List<News>();
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(_url);
HtmlNode ulNode = doc.DocumentNode.SelectSingleNode("//ul[#class='content articles']");
HtmlNodeCollection liNode = ulNode.SelectNodes(".//li");
foreach (HtmlNode node in ulNode.SelectNodes(".//div[#class='article_box']"))
{
var news = new News();
news.Imgsrc = node.FirstChild.SelectSingleNode("//img").Attributes["src"].Value;
var nodes = doc.DocumentNode.FirstChild.SelectNodes("//img[#src]");
foreach (HtmlNode childNode in node.SelectNodes(".//div[#class='box_info']"))
{
// string src = node.SelectSingleNode("//img").Attributes["src"].Value;
foreach(HtmlNode _node in childNode.SelectNodes(".//h3"))
{
news.Link = "";
news.Title = _node.FirstChild.InnerText;
news.Date = _node.NextSibling.NextSibling.InnerText;
news.Text = _node.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
}
}
_newsList.Add(news);
newsCounter++;
}
and also the News class :
public class News
{
public string Imgsrc { get; set; }
public string Title { get; set; }
public string Link { get; set; }
public string Date { get; set; }
public string Text { get; set; }
}
these are all the parameters i have to read from the list.I am able to read them and return all of the news in my list , but what next , how to transform my list into xml format? Any suggestions are welcomed.
There are many way of creating xml. There are not a lot of items in your case so just using Xml linq is very simple. Putting it into a class may produce cleaner code or you can just use the code directly like Sledge suggested.
public class News
{
public string Imgsrc { get; set; }
public string Title { get; set; }
public string Link { get; set; }
public string Date { get; set; }
public string Text { get; set; }
public XElement ToXml()
{
return new XElement("news", new object[] {
new XElement("Imgscr", Imgsrc),
new XElement("Title", Title),
new XElement("Link", Link),
new XElement("Date", Date),
new XElement("Text", Text),
});
}
}
Thanks to everyone guys. I marked 'News' class as Serializable and with a few lines of code managed to generate the xml file. Here is the code, really simple :
XmlSerializer serializer = new XmlSerializer(typeof(List<News>));
using (TextWriter writer = new StreamWriter(#"D:\News.xml"))
{
serializer.Serialize(writer, _newsList);
}
I'm trying to deserialize a given JSON file in C# using a tutorial by Bill Reiss. For XML data in a non-list this method works pretty well, though I would like to deserialize a JSON file with the following structure:
public class Data
{
public string Att1 { get; set; }
public string Att2 { get; set; }
public string Att3 { get; set; }
public string Att4 { get; set; }
}
public class RootObject
{
public List<Data> Listname { get; set; }
}
My problem is with using JSON.Net's ability to create / put data into lists, and then displaying the list on an XAML page. My idea so far (which is not working):
var resp = await client.DoRequestJsonAsync<DATACLASS>("URL");
string t = resp.ToString();
var _result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DATACLASS>>(t);
XAMLELEMENT.ItemsSource = _result;
So I think you're probably trying to deserialize to the wrong type. If you serialized it to RootObject and try to deserialize to List it's going to fail.
See this example code
public void TestMethod1()
{
var items = new List<Item>
{
new Item { Att1 = "ABC", Att2 = "123" },
new Item { Att1 = "EFG", Att2 = "456" },
new Item { Att1 = "HIJ", Att2 = "789" }
};
var root = new Root() { Items = items };
var itemsSerialized = JsonConvert.SerializeObject(items);
var rootSerialized = JsonConvert.SerializeObject(root);
//This works
var deserializedItemsFromItems = JsonConvert.DeserializeObject<List<Item>>(itemsSerialized);
//This works
var deserializedRootFromRoot = JsonConvert.DeserializeObject<Root>(rootSerialized);
//This will fail. YOu serialized it as root and tried to deserialize as List<Item>
var deserializedItemsFromRoot = JsonConvert.DeserializeObject<List<Item>>(rootSerialized);
//This will fail also for the same reason
var deserializedRootFromItems = JsonConvert.DeserializeObject<Root>(itemsSerialized);
}
class Root
{
public IEnumerable<Item> Items { get; set; }
}
class Item
{
public string Att1 { get; set; }
public string Att2 { get; set; }
}
Edit: Added complete code.
I want to create XML document with multiple elements inside. The format should be something like this:
<Item>
<Id>2276138</Id>
<Title>92907-03100-00 WASHER, CHAIN</Title>
<Link>http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00</Link>
<Price>0.0000</Price>
<Description>WASHER, CHAIN (92907-03100-00)</Description>
<Condition>New</Condition>
<Brand />
<Product_Type />
<Availability>In Stock</Availability>
<Manufacturer>Suzuki</Manufacturer>
</Item>
Everything is ok after the first loop of fetching data, but once the 2nd loop is done, I have this:
<Item></Item>
<Item>
<Id>2276138</Id>
<Title>92907-03100-00 WASHER, CHAIN</Title>
<Link>http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00</Link>
<Price>0.0000</Price>
<Description>WASHER, CHAIN (92907-03100-00)</Description>
<Condition>New</Condition>
<Brand />
<Product_Type />
<Availability>In Stock</Availability>
<Manufacturer>Suzuki</Manufacturer>
</Item>
So after every round of fetching I get the empty item element and just the last element is populated.
Here is the code for the loop:
while (rdr.Read())
{
if (rdr.HasRows)
{
XmlElement part = docOrders.CreateElement("Item");
id.InnerText = rdr[0].ToString();
part.AppendChild(id);
title.InnerText = rdr[1].ToString();
part.AppendChild(title);
link.InnerText = rdr[2].ToString();
part.AppendChild(link);
price.InnerText = rdr[3].ToString();
part.AppendChild(price);
desc.InnerText = rdr[4].ToString();
part.AppendChild(desc);
cond.InnerText = rdr[5].ToString();
part.AppendChild(cond);
brand.InnerText = rdr[6].ToString();
part.AppendChild(brand);
productType.InnerText = rdr[7].ToString();
part.AppendChild(productType);
availability.InnerText = rdr[8].ToString();
part.AppendChild(availability);
manufacturer.InnerText = rdr[9].ToString();
part.AppendChild(manufacturer);
root.AppendChild(part);
}
}
rdr.Close();
}
How Can I solve this, so the data will be fetched correctly?
Thanks in advance
where do you create the id, title, etc. nodes? It looks like you're reusing these instead of creating new nodes, that's why it's not working correctly.
If you're reusing a child node, it will remove it from the current node and insert it into the new node, that's why you're seeing the empty element.
Also check this question here on SO, basically the same exact problem.
You haven't said which version of .NET you are using. If using .NET 3.5 or above, then I would recommend using LINQ to XML, especially if you can get strongly-typed data from your query. For example:
using System;
using System.Linq;
using System.Xml.Linq;
public class Testing
{
private void Main()
{
var items = new[]
{
new DataItem
{
Id = 2276138,
Title = "92907-03100-00 WASHER, CHAIN",
Link =
new Uri(
"http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00"),
Price = 0.0M,
Description = "WASHER, CHAIN (92907-03100-00)",
Condition = "New",
Availability = "In Stock",
Manufacturer = "Suzuki"
},
new DataItem
{
Id = 2276139,
Title = "92907-03100-01 WASHER, CHAIN",
Link =
new Uri(
"http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00"),
Price = 0.0M,
Description = "WASHER, CHAIN (92907-03100-00)",
Condition = "New",
Availability = "In Stock",
Manufacturer = "Suzuki"
},
new DataItem
{
Id = 2276140,
Title = "92907-03100-02 WASHER, CHAIN",
Link =
new Uri(
"http://www.mywebsite.com/Product.aspx?ProductId=2453575&SKU=92907-03100-00"),
Price = 0.0M,
Description = "WASHER, CHAIN (92907-03100-00)",
Condition = "New",
Availability = "In Stock",
Manufacturer = "Suzuki"
},
};
var doc = new XDocument(
new XElement(
"Items",
from item in items
select
new XElement(
"Item",
new XElement("Id", item.Id),
new XElement("Title", item.Title),
new XElement("Link", item.Link),
new XElement("Price", item.Price),
new XElement("Description", item.Description),
new XElement("Condition", item.Condition),
new XElement("Brand", item.Brand),
new XElement("Product_Type", item.ProductType),
new XElement("Availability", item.Availability),
new XElement("Manufacturer", item.Manufacturer))));
}
public class DataItem
{
public int Id { get; set; }
public string Title { get; set; }
public Uri Link { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string Condition { get; set; }
public string Brand { get; set; }
public string ProductType { get; set; }
public string Availability { get; set; }
public string Manufacturer { get; set; }
}
}
Have you considered using the System.XML.Serialisation namespaces, which has an XMLSerializer class which does this kind of thing very well for you? There's some MSDN documentation here - http://msdn.microsoft.com/en-us/library/swxzdhc0.aspx - which goes into depth with some good examples, and a shorter description here which has enough for the basics - http://support.microsoft.com/kb/815813
Given the following XML:
<?xml version="1.0"?>
<user_list>
<user>
<id>1</id>
<name>Joe</name>
</user>
<user>
<id>2</id>
<name>John</name>
</user>
</user_list>
And the following class:
public class User {
[XmlElement("id")]
public Int32 Id { get; set; }
[XmlElement("name")]
public String Name { get; set; }
}
Is it possible to use XmlSerializer to deserialize the xml into a List<User> ? If so, what type of additional attributes will I need to use, or what additional parameters do I need to use to construct the XmlSerializer instance?
An array ( User[] ) would be acceptable, if a bit less preferable.
You can encapsulate the list trivially:
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("user_list")]
public class UserList
{
public UserList() {Items = new List<User>();}
[XmlElement("user")]
public List<User> Items {get;set;}
}
public class User
{
[XmlElement("id")]
public Int32 Id { get; set; }
[XmlElement("name")]
public String Name { get; set; }
}
static class Program
{
static void Main()
{
XmlSerializer ser= new XmlSerializer(typeof(UserList));
UserList list = new UserList();
list.Items.Add(new User { Id = 1, Name = "abc"});
list.Items.Add(new User { Id = 2, Name = "def"});
list.Items.Add(new User { Id = 3, Name = "ghi"});
ser.Serialize(Console.Out, list);
}
}
If you decorate the User class with the XmlType to match the required capitalization:
[XmlType("user")]
public class User
{
...
}
Then the XmlRootAttribute on the XmlSerializer ctor can provide the desired root and allow direct reading into List<>:
// e.g. my test to create a file
using (var writer = new FileStream("users.xml", FileMode.Create))
{
XmlSerializer ser = new XmlSerializer(typeof(List<User>),
new XmlRootAttribute("user_list"));
List<User> list = new List<User>();
list.Add(new User { Id = 1, Name = "Joe" });
list.Add(new User { Id = 2, Name = "John" });
list.Add(new User { Id = 3, Name = "June" });
ser.Serialize(writer, list);
}
...
// read file
List<User> users;
using (var reader = new StreamReader("users.xml"))
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<User>),
new XmlRootAttribute("user_list"));
users = (List<User>)deserializer.Deserialize(reader);
}
Credit: based on answer from YK1.
Yes, it will serialize and deserialize a List<>. Just make sure you use the [XmlArray] attribute if in doubt.
[Serializable]
public class A
{
[XmlArray]
public List<string> strings;
}
This works with both Serialize() and Deserialize().
I think I have found a better way. You don't have to put attributes into your classes. I've made two methods for serialization and deserialization which take generic list as parameter.
Take a look (it works for me):
private void SerializeParams<T>(XDocument doc, List<T> paramList)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(paramList.GetType());
System.Xml.XmlWriter writer = doc.CreateWriter();
serializer.Serialize(writer, paramList);
writer.Close();
}
private List<T> DeserializeParams<T>(XDocument doc)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
System.Xml.XmlReader reader = doc.CreateReader();
List<T> result = (List<T>)serializer.Deserialize(reader);
reader.Close();
return result;
}
So you can serialize whatever list you want! You don't need to specify the list type every time.
List<AssemblyBO> list = new List<AssemblyBO>();
list.Add(new AssemblyBO());
list.Add(new AssemblyBO() { DisplayName = "Try", Identifier = "243242" });
XDocument doc = new XDocument();
SerializeParams<T>(doc, list);
List<AssemblyBO> newList = DeserializeParams<AssemblyBO>(doc);
Yes, it does deserialize to List<>. No need to keep it in an array and wrap/encapsulate it in a list.
public class UserHolder
{
private List<User> users = null;
public UserHolder()
{
}
[XmlElement("user")]
public List<User> Users
{
get { return users; }
set { users = value; }
}
}
Deserializing code,
XmlSerializer xs = new XmlSerializer(typeof(UserHolder));
UserHolder uh = (UserHolder)xs.Deserialize(new StringReader(str));
Not sure about List<T> but Arrays are certainly do-able. And a little bit of magic makes it really easy to get to a List again.
public class UserHolder {
[XmlElement("list")]
public User[] Users { get; set; }
[XmlIgnore]
public List<User> UserList { get { return new List<User>(Users); } }
}
How about
XmlSerializer xs = new XmlSerializer(typeof(user[]));
using (Stream ins = File.Open(#"c:\some.xml", FileMode.Open))
foreach (user o in (user[])xs.Deserialize(ins))
userList.Add(o);
Not particularly fancy but it should work.
Yes, you can deserialize into List<User> or User[] with the XmlSerializer. I would prefer List<User> over User[]. Note that XmlSerializer does not support deserialization into interfaces, so you cannot deserialize into ICollection<T>, IReadOnlyCollection<T> or IList<T> as this will fail with a NotSupportedException.