XMLElement and Type Parse.

S

shapper

Hello,

I am writing/reading data to/from a XML file using XMLDocument and
XMLElement.
I am confused on the data conversions. For example:

This is how I create an XElement:

XElement product = new XElement("Message",
new XElement("Id", id.ToString()),
new XElement("Description", product.Description),
new XElement("Active", product.Active.ToString())
);

And this is how I read it:

Product product = _products.Root.Elements("Product").Select(p =>
new Product {
Id = XmlConvert.ToInt32(m.Element("Id").Value),
Description = m.Element("Content").Value,
Active = Boolean.Parse(r.Element("Active").Value)
}).FirstOrDefault(p => p.Id == id);

My product model is something like:

public class Product {
public Int32 Id { get; set; }
public String Description { get; set; }
public Boolean Active { get; set; }
}

I have other models that contain other types like Base64String,
DateTime, Double, etc

1. When creating a new XElement I use ToString() to convert any type,
other then string, like DateTime, Boolean, Int, etc. When I need to
save a Base64String so I use Convert.ToBase64String();
Maybe I don't need to do this?

2. When getting the data from the XElement to the model I am using
Int32.Parse, Boolean.Parse, DateTime.Parse, etc. When getting a
Base64String I use Convert.FromBase64String(),

What is the proper way to do this?

Use XmlConvert instead of parse when reading the values?
I was checking XMLConvert and I have a lot of options: ToInt32,
ToBoolean, ToDateTime, etc

So I should use XMLConvert for these cases? I didn't know about
XMLConvert.
Use nothing when the value of the model is string
Use Convert.FromBase64String when is a Base64String.

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
I am writing/reading data to/from a XML file using XMLDocument and
XMLElement.

Here you are mentioning XMLDocument and XMLElement yet below you use
something very different, namely XElement. XElement (and XDocument) are
new in .NET 3.5, part of LINQ to XML. There are no types named
XMLDocument and XMLElement in the .NET framework but there is
System.Xml.XmlDocument/XmlElement.
I am confused on the data conversions. For example:

This is how I create an XElement:

XElement product = new XElement("Message",
new XElement("Id", id.ToString()),

It depends on what you want to achieve but if you want to store data in
XML then I would strongly suggest to not use ToString() on number values
or DateTimes as I think that way you will get a culture dependent string
representation. It might not matter for integers but for doubles it matters.
And you don't need ToString, if you look at the XElement constructor
then you will see that it takes type object for the second argument and
http://msdn.microsoft.com/en-us/library/bb943882.aspx explains what you
can pass in there and the constructor knows how to deal with.
So e.g.

new XElement("Id", id)

should suffice, then when you want the id value back simply use

(int)m.Element("Id")


Use XmlConvert instead of parse when reading the values?
I was checking XMLConvert and I have a lot of options: ToInt32,
ToBoolean, ToDateTime, etc

XmlConvert is older than LINQ to XML and I am sure LINQ to XML uses it
under the hood for the ways I have outlined above (passing in the a
value to the XElement constructor respectively casting an XElement) but
you don't need to use it explicitly with LINQ to XML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top