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?To: NULL Date: 8/4/2009 2:36:00 PM On Jul 24, 7:05=A0am, Martin Honnen <mahotr...@yahoo.de> wrote:
> I am not sure what you are doing differently but somehow your code must
> be different that it skips every even item.
>
Today, just for fun, I tried similar code in C# Forms rather than C#
ASP.NET but it again skips every other node (even nodes), just like in
ASP.NET. Thus only first names Rob1 and Rob3 are being read back from
the XML file, but not Rob2. Strange how only Rob2 shows up in
Debug.Writeline, see below.
Too bad, as this "short way" would have been nice. Strange but in
console mode I don't have any problems. One possible problem: perhaps
in C# when </MyEmployee> is encountered, somehow the node skips? But
why not this problem in Console mode?
BTW, do...while does not seem to affect things differently (rather
than just while) nor does .localname vs .name
RL
private void button1_Click(object sender, EventArgs e)
{
DecoratedPerson myDecPerson1 =3D new DecoratedPerson();
myDecPerson1.FirstName =3D "Rob1";
myDecPerson1.LastName =3D "Smith1";
myDecPerson1.Password =3D "SecretSmith";
myDecPerson1.Email =3D "Smith1@a...";
myDecPerson1.Age =3D int.Parse("19");
myDecPerson1.UserId =3D "Rob1Smith";
Guid myGuid =3D new Guid();
myGuid =3D System.Guid.NewGuid();
myDecPerson1.UserGuid =3D myGuid;
// add to List
myDecPersonList.Add(myDecPerson1);
//NOTE: http://groups.google.com/group/comp.text.xml/browse_th=
read/thread/41bc4904526329da?hl=3Den#
DecoratedPerson myDecPerson2 =3D new DecoratedPerson();
myDecPerson2.FirstName =3D "Rob2T";
myDecPerson2.LastName =3D "Smith2T";
myDecPerson2.Password =3D "SecretSmith2T";
myDecPerson2.Email =3D "Smith2T@a...";
myDecPerson2.Age =3D int.Parse("188");
myDecPerson2.UserId =3D "Rob2TSmith";
myGuid =3D new Guid();
myGuid =3D System.Guid.NewGuid();
myDecPerson2.UserGuid =3D myGuid;
myDecPersonList.Add(myDecPerson2);
DecoratedPerson myDecPerson3 =3D new DecoratedPerson();
myDecPerson3.FirstName =3D "Rob3";
myDecPerson3.LastName =3D "Smith3";
myDecPerson3.Password =3D "SecretSmith3";
myDecPerson3.Email =3D "Smith3@a...";
myDecPerson3.Age =3D int.Parse("333");
myDecPerson3.UserId =3D "Rob3Smith";
myGuid =3D new Guid();
myGuid =3D System.Guid.NewGuid();
myDecPerson3.UserGuid =3D myGuid;
myDecPersonList.Add(myDecPerson3);
string totalFilepath =3D System.IO.Path.GetDirectoryName
(System.Reflection.Assembly.GetExecutingAssembly().Location);
totalFilepath +=3D @"\MyXMLfile.xml"; //add the name of the
file
Debug.WriteLine("Thsi is the path: " + totalFilepath);
XmlSerializerNamespaces ns =3D new XmlSerializerNamespaces
();
//ns.Add("", "");
ns.Add("mytest", "http://www.w3.org/2001/XMLSchemaMyOwn");
XmlWriterSettings settings =3D new XmlWriterSettings();
settings.OmitXmlDeclaration =3D false; // Remove the <?xml
version=3D"1.0" encoding=3D"utf-8"?>
XmlWriter writer =3D XmlWriter.Create(totalFilepath,
settings); //create used: // culver NOTE TO DO: if you wanted to add
to the end of an existing XML file you would have to read the file
first into memory...
writer.WriteStartElement("ARootElement");
// FileStream stream =3D new FileStream(totalFilepath,
FileMode.Create);
XmlSerializer serializer =3D new XmlSerializer(typeof
(DecoratedPerson));
foreach (DecoratedPerson p in myDecPersonList)
{
serializer.Serialize(writer, p, ns);
}
writer.WriteEndElement(); //writes </ARootElement>
automatically
writer.Close();
//stream.Close();
}
private void button2_Click(object sender, EventArgs e)
{
////deserialize
List<DecoratedPerson> persons =3D new List<DecoratedPerson>
();
XmlSerializer ser =3D new XmlSerializer(typeof
(DecoratedPerson));
string totalFilepath =3D System.IO.Path.GetDirectoryName
(System.Reflection.Assembly.GetExecutingAssembly().Location);
totalFilepath +=3D @"\MyXMLfile.xml"; //add the name of the
file
try
{
using (XmlReader reader =3D XmlReader.Create
(totalFilepath))
{
DecoratedPerson p;
do
{
Debug.WriteLine("reader is: " + reader.Name +
"," + reader.NodeType.ToString() + "," + reader.LocalName + "," +
reader.ValueType.ToString() + "," + reader.Value.ToString());
if (reader.NodeType =3D=3D XmlNodeType.Element &&
reader.Name =3D=3D "MyEmployee")
{
p =3D (DecoratedPerson)ser.Deserialize
(reader);
persons.Add(p);
}
}
while (reader.Read());
reader.Close();
}
}
catch (FileNotFoundException fNF)
{
Debug.WriteLine(fNF.Message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
Console.WriteLine("Found {0} items:", persons.Count);
foreach (DecoratedPerson p in persons)
{
Debug.WriteLine(p.FirstName);
}
}
//
reader is: xml,XmlDeclaration,xml,System.String,version=3D"1.0"
encoding=3D"utf-8"
reader is: ARootElement,Element,ARootElement,System.String,
reader is: MyEmployee,Element,MyEmployee,System.String,
reader is: FiRstName,Element,FiRstName,System.String,
reader is: ,Text,,System.String,Rob2T
reader is: FiRstName,EndElement,FiRstName,System.String,
reader is: LAsTNaMe,Element,LAsTNaMe,System.String,
reader is: ,Text,,System.String,Smith2T
reader is: LAsTNaMe,EndElement,LAsTNaMe,System.String,
reader is: thePassword,Element,thePassword,System.String,
reader is: ,Text,,System.String,SecretSmith2T
reader is: thePassword,EndElement,thePassword,System.String,
reader is: theEmaiL,Element,theEmaiL,System.String,
reader is: ,Text,,System.String,Smith2T@a...
reader is: theEmaiL,EndElement,theEmaiL,System.String,
reader is: Age,Element,Age,System.String,
reader is: ,Text,,System.String,188
reader is: Age,EndElement,Age,System.String,
reader is: UserId,Element,UserId,System.String,
reader is: ,Text,,System.String,Rob2TSmith
reader is: UserId,EndElement,UserId,System.String,
reader is: MyEmployee,EndElement,MyEmployee,System.String,
reader is: MyEmployee,Element,MyEmployee,System.String,
// only nodes 1, 3 are read back! Not node 2
Rob1
Rob3
// XML file (which is correctly written) here:
<?xml version=3D"1.0" encoding=3D"utf-8" ?>
- <ARootElement>
- <MyEmployee xmlns:mytest=3D"http://www.w3.org/2001/XMLSchemaMyOwn"
GUID=3D"84b9a753-8106-4870-a7a3-6814a0742bf5">
<FiRstName>Rob1</FiRstName>
<LAsTNaMe>Smith1</LAsTNaMe>
<thePassword>SecretSmith</thePassword>
<theEmaiL>Smith1@a...</theEmaiL>
<Age>19</Age>
<UserId>Rob1Smith</UserId>
</MyEmployee>
- <MyEmployee xmlns:mytest=3D"http://www.w3.org/2001/XMLSchemaMyOwn"
GUID=3D"2d05ddc3-ff2e-45f5-b350-a37bd6d6bb95">
<FiRstName>Rob2T</FiRstName>
<LAsTNaMe>Smith2T</LAsTNaMe>
<thePassword>SecretSmith2T</thePassword>
<theEmaiL>Smith2T@a...</theEmaiL>
<Age>188</Age>
<UserId>Rob2TSmith</UserId>
</MyEmployee>
- <MyEmployee xmlns:mytest=3D"http://www.w3.org/2001/XMLSchemaMyOwn"
GUID=3D"d679e964-08fe-45fa-8264-42fa9e9e3307">
<FiRstName>Rob3</FiRstName>
<LAsTNaMe>Smith3</LAsTNaMe>
<thePassword>SecretSmith3</thePassword>
<theEmaiL>Smith3@a...</theEmaiL>
<Age>333</Age>
<UserId>Rob3Smith</UserId>
</MyEmployee>
</ARootElement>
// decorated class here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace XmlForms
{
[XmlRoot(ElementName =3D "MyEmployee")]
public class DecoratedPerson
{
private string firstName;
private string lastName;
private string password;
private string email;
private int age;
private Guid userGuid;
private string userId;
[XmlElement(ElementName =3D "FiRstName")]
public string FirstName
{
get
{
return firstName;
}
set
{
firstName =3D value;
}
}
[XmlElement(ElementName =3D "LAsTNaMe")]
public string LastName
{
get
{
return lastName;
}
set
{
lastName =3D value;
}
}
[XmlElement(ElementName =3D "thePassword")]
public string Password
{
get
{
return password;
}
set
{
password =3D value;
}
}
[XmlElement(ElementName =3D "theEmaiL")]
public string Email
{
get
{
return email;
}
set
{
email =3D value;
}
}
[XmlElement(IsNullable =3D false)]
public int Age
{
get
{
return age;
}
set
{
age =3D value;
}
}
[XmlAttribute(AttributeName =3D "GUID")]
public Guid UserGuid
{
get
{
return userGuid;
}
set
{
userGuid =3D value;
}
}
[XmlElement(IsNullable =3D true)]
public string UserId
{
get
{
return userId;
}
set
{
userId =3D value;
}
}
public DecoratedPerson()
{
firstName =3D String.Empty;
lastName =3D String.Empty;
password =3D String.Empty;
email =3D String.Empty;
age =3D -1;
userGuid =3D new Guid();
userId =3D String.Empty;
}
}
}
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
