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
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