R
RayLopez99
Is there an easy way to do this? Without using style sheet filters,
etc? I have a decorated class (uses [XmlRoot], [XmlAttribute], etc)
but I can only add one node at a time when I create the XML file--but
I want to add more than one node, however I get an error if you add
more than one node (error: cannot form XML).
If there's a short way to fix this please let me know. I think I can
fix it using DOM and the "long or hard way", by writing and reading
each node individually from a List (I've done this before). But I'm
trying to do this the "short or easy way" using XML Serialization.
Thank you
RL
//Here is the code: self explanitory, two Decorated class nodes being
added, myDecPerson1 ("Rob1 Smith") and myDecPerson1 ("Rob2 Smith2")
// Discussion on stripping out top element here: //
http://www.csharper.net/blog/serializing_without_the_namespace__xmlns__xmlns_xsd__xmlns_xsi_.aspx
protected void cmd_Button1_Click(Object sender, EventArgs e)
{
DecoratedPerson myDecPerson1 = new DecoratedPerson();
myDecPerson1.FirstName = "Rob1";
myDecPerson1.LastName = "Smith1";
myDecPerson1.Password = "SecretSmith";
myDecPerson1.Email = "(e-mail address removed)";
myDecPerson1.Age = int.Parse("99");
myDecPerson1.UserId = "Rob1Smith";
Guid myGuid = new Guid();
myGuid = System.Guid.NewGuid();
myDecPerson1.UserGuid = myGuid;
// add to List
myDecPersonList.Add(myDecPerson1);
// now do second person, and add to list--fails!...
DecoratedPerson myDecPerson2 = new DecoratedPerson();
myDecPerson2.FirstName = "Rob2";
myDecPerson2.LastName = "Smith2";
myDecPerson2.Password = "SecretSmith2";
myDecPerson2.Email = "(e-mail address removed)";
myDecPerson2.Age = int.Parse("88");
myDecPerson2.UserId = "Rob2Smith";
myGuid = new Guid();
myGuid = System.Guid.NewGuid();
myDecPerson2.UserGuid = myGuid;
myDecPersonList.Add(myDecPerson2);
//create file and add nodes ..
string totalFilepath = Path.Combine
(Request.PhysicalApplicationPath, @"App_Data
\XMLPasswordDoc77.xml"); //create path
XmlSerializerNamespaces ns = new XmlSerializerNamespaces
();
ns.Add("mytest", "http://www.w3.org/2001/XMLSchemaMyOwn");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = false; // Don't remove the
<?xml version="1.0" encoding="utf-8"?>
XmlWriter writer = XmlWriter.Create(totalFilepath,
settings);
XmlSerializer serializer = new XmlSerializer(typeof
(DecoratedPerson));
foreach (DecoratedPerson p in myDecPersonList)
{
serializer.Serialize(writer, p, ns); //only works w/o
exception if only one element in list! if > 1 fails!
}
writer.Close();
} //end of method
//OUTPUT (works if only one node in List):
//
<?xml version="1.0" encoding="utf-8" ?>
- <MyEmployee xmlns:mytest="http://www.w3.org/2001/XMLSchemaMyOwn"
GUID="665ae831-275e-427b-9737-9f3856c8c84a">
<FiRstName>Rob1</FiRstName>
<lAsTNaMe>Smith1</lAsTNaMe>
<pAssworD>SecretSmith</pAssworD>
<eeemaiL>[email protected]</eeemaiL>
<Age>99</Age>
<UserId>Rob1Smith</UserId>
</MyEmployee>
// but error if you add more than one node (cannot form XML)
//Here is the decorated class...
[XmlRoot(ElementName="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="FiRstName")]
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
[XmlElement(ElementName="lAsTNaMe")]
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
[XmlElement(ElementName = "pAssworD")]
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
[XmlElement(ElementName = "eeemaiL")]
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
[XmlElement(IsNullable=false)]
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
[XmlAttribute(AttributeName="GUID")]
public Guid UserGuid
{
get
{
return userGuid;
}
set
{
userGuid = value;
}
}
[XmlElement(IsNullable = true)]
public string UserId
{
get
{
return userId;
}
set
{
userId = value;
}
}
public DecoratedPerson()
{
}
}
etc? I have a decorated class (uses [XmlRoot], [XmlAttribute], etc)
but I can only add one node at a time when I create the XML file--but
I want to add more than one node, however I get an error if you add
more than one node (error: cannot form XML).
If there's a short way to fix this please let me know. I think I can
fix it using DOM and the "long or hard way", by writing and reading
each node individually from a List (I've done this before). But I'm
trying to do this the "short or easy way" using XML Serialization.
Thank you
RL
//Here is the code: self explanitory, two Decorated class nodes being
added, myDecPerson1 ("Rob1 Smith") and myDecPerson1 ("Rob2 Smith2")
// Discussion on stripping out top element here: //
http://www.csharper.net/blog/serializing_without_the_namespace__xmlns__xmlns_xsd__xmlns_xsi_.aspx
protected void cmd_Button1_Click(Object sender, EventArgs e)
{
DecoratedPerson myDecPerson1 = new DecoratedPerson();
myDecPerson1.FirstName = "Rob1";
myDecPerson1.LastName = "Smith1";
myDecPerson1.Password = "SecretSmith";
myDecPerson1.Email = "(e-mail address removed)";
myDecPerson1.Age = int.Parse("99");
myDecPerson1.UserId = "Rob1Smith";
Guid myGuid = new Guid();
myGuid = System.Guid.NewGuid();
myDecPerson1.UserGuid = myGuid;
// add to List
myDecPersonList.Add(myDecPerson1);
// now do second person, and add to list--fails!...
DecoratedPerson myDecPerson2 = new DecoratedPerson();
myDecPerson2.FirstName = "Rob2";
myDecPerson2.LastName = "Smith2";
myDecPerson2.Password = "SecretSmith2";
myDecPerson2.Email = "(e-mail address removed)";
myDecPerson2.Age = int.Parse("88");
myDecPerson2.UserId = "Rob2Smith";
myGuid = new Guid();
myGuid = System.Guid.NewGuid();
myDecPerson2.UserGuid = myGuid;
myDecPersonList.Add(myDecPerson2);
//create file and add nodes ..
string totalFilepath = Path.Combine
(Request.PhysicalApplicationPath, @"App_Data
\XMLPasswordDoc77.xml"); //create path
XmlSerializerNamespaces ns = new XmlSerializerNamespaces
();
ns.Add("mytest", "http://www.w3.org/2001/XMLSchemaMyOwn");
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = false; // Don't remove the
<?xml version="1.0" encoding="utf-8"?>
XmlWriter writer = XmlWriter.Create(totalFilepath,
settings);
XmlSerializer serializer = new XmlSerializer(typeof
(DecoratedPerson));
foreach (DecoratedPerson p in myDecPersonList)
{
serializer.Serialize(writer, p, ns); //only works w/o
exception if only one element in list! if > 1 fails!
}
writer.Close();
} //end of method
//OUTPUT (works if only one node in List):
//
<?xml version="1.0" encoding="utf-8" ?>
- <MyEmployee xmlns:mytest="http://www.w3.org/2001/XMLSchemaMyOwn"
GUID="665ae831-275e-427b-9737-9f3856c8c84a">
<FiRstName>Rob1</FiRstName>
<lAsTNaMe>Smith1</lAsTNaMe>
<pAssworD>SecretSmith</pAssworD>
<eeemaiL>[email protected]</eeemaiL>
<Age>99</Age>
<UserId>Rob1Smith</UserId>
</MyEmployee>
// but error if you add more than one node (cannot form XML)
//Here is the decorated class...
[XmlRoot(ElementName="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="FiRstName")]
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
[XmlElement(ElementName="lAsTNaMe")]
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
[XmlElement(ElementName = "pAssworD")]
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
[XmlElement(ElementName = "eeemaiL")]
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
[XmlElement(IsNullable=false)]
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
[XmlAttribute(AttributeName="GUID")]
public Guid UserGuid
{
get
{
return userGuid;
}
set
{
userGuid = value;
}
}
[XmlElement(IsNullable = true)]
public string UserId
{
get
{
return userId;
}
set
{
userId = value;
}
}
public DecoratedPerson()
{
}
}