Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: Append to a file using XML Serialization? Easy way to do this? >Thread Next - Re: Append to a file using XML Serialization? Easy way to do this? Re: Append to a file using XML Serialization? Easy way to do this?To: NULL Date: 7/20/2009 3:08:00 PM Also does anybody know if there is a 'easier' (or cleaner, less code
required) way of deserializing than checking each node as per the
below code?
RL
//as inspired by: http://support.microsoft.com/kb/307548
private string ReadXMLandAddtoDecoratedPersonList(string filepath)
{
string statusStr = "Success
ReadXMLandAddtoDecoratedPersonList";
try
{
using (FileStream fs = new FileStream(filepath,
FileMode.Open))
{
using (XmlTextReader r = new XmlTextReader(fs))
{
myDecPersonList.Clear();
DecoratedPerson newDecPerson1 = new
DecoratedPerson();
while (r.Read())
{
Debug.WriteLine("r.Read element is: " +
r.NodeType.ToString() + "(type)" + "," + r.ValueType.ToString() +
" (value type)" + "," + r.Value.ToString() + "(value)" + "," +
r.Name.ToString() + "Name!");
if (r.NodeType == XmlNodeType.Element &&
r.Name == "MyEmployee")
{
string aStringGuid = r.GetAttribute
(1);//gets Second attribute, which is GUID (.GetAttribute(0) is
schema)
Debug.WriteLine("this is MyEmployee
attribute: " + aStringGuid);
newDecPerson1.UserGuid = new Guid
(aStringGuid);
}
if (r.NodeType == XmlNodeType.Element &&
r.Name == "FiRstName")
{
r.Read(); // req'd to push to next
element
if (r.NodeType == XmlNodeType.Text)
{
newDecPerson1.FirstName = r.Value;
Debug.WriteLine("r.Read element
FirstName is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");
}
}
if (r.NodeType == XmlNodeType.Element &&
r.Name == "LAsTNaMe")
{
r.Read(); //!! req'd to push to next
element
if (r.NodeType == XmlNodeType.Text)
{
newDecPerson1.LastName = r.Value;
Debug.WriteLine("r.Read element
LastName is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");
}
}
if (r.NodeType == XmlNodeType.Element &&
r.Name == "thePassword")
{
r.Read(); //!! req'd to push to next
element
if (r.NodeType == XmlNodeType.Text)
{
newDecPerson1.Password = r.Value;
Debug.WriteLine("r.Read element
Password is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");
}
}
if (r.NodeType == XmlNodeType.Element &&
r.Name == "theEmaiL")
{
r.Read(); //!! req'd to push to next
element
if (r.NodeType == XmlNodeType.Text)
{
newDecPerson1.Email = r.Value;
Debug.WriteLine("r.Read element
Email is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");
}
}
if (r.NodeType == XmlNodeType.Element &&
r.Name == "Age")
{
r.Read(); //!! req'd to push to next
element
if (r.NodeType == XmlNodeType.Text)
{
try
{
newDecPerson1.Age =
Convert.ToInt32(r.Value);
Debug.WriteLine("r.Read
element Age is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");
}
catch (FormatException)
{
Debug.WriteLine("The Age did
not convert: default zero used");
newDecPerson1.Age = 0;
}
}
}
if (r.NodeType == XmlNodeType.Element &&
r.Name == "UserId")
{
r.Read(); //!! req'd to push to next
element
if (r.NodeType == XmlNodeType.Text)
{
newDecPerson1.UserId = r.Value;
Debug.WriteLine("r.Read element
UserId is: " + r.NodeType.ToString() + "(type)" + "," +
r.ValueType.ToString() + " (value type)" + "," + r.Value.ToString() +
"(value)");
}
}
if ((newDecPerson1.FirstName !=
String.Empty) && (newDecPerson1.LastName != String.Empty) &&
(newDecPerson1.Password != String.Empty) && (newDecPerson1.Email !=
String.Empty) && (newDecPerson1.Age != -1) && (newDecPerson1.UserId !
= String.Empty))
{
//add new person to the list and
create a new newDecPerson
myDecPersonList.Add(newDecPerson1);
Debug.WriteLine("now adding a
DecoratedPerson comprising...");
Debug.WriteLine
(newDecPerson1.FirstName + "," + newDecPerson1.LastName + "," +
newDecPerson1.Password + "," + newDecPerson1.Email + "," +
newDecPerson1.Age.ToString() + "," + newDecPerson1.UserId);
newDecPerson1 = new DecoratedPerson
(); //re-instantiate a new object, since old one added to List.
}
}
}
}
}
catch (FileNotFoundException fNF)
{
Debug.WriteLine(fNF.Message);
statusStr = fNF.Message;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
statusStr = ex.Message;
}
finally
{
////debug output: works fine, as expected
foreach (DecoratedPerson p in myDecPersonList)
{
Debug.WriteLine("these values exist for p: " +
p.LastName + "," + p.FirstName + "," + p.Password + "," + p.Email +
"," + p.Age.ToString() + "," + p.UserId + "," + p.UserGuid);
}
}
return statusStr;
}
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
