Reputation: 6278
i need to replicate the following in XML, but unsure how to do this:
<FAMILY>
<NAME>
<AGE>
<DATEOFBIRTH>
<NAME>
<AGE>
<DATEOFBIRTH>
<NAME>
<AGE>
<DATEOFBIRTH>
<NAME>
<AGE>
<DATEOFBIRTH>
</FAMILY>
i'm using a very basic example to explain what i need assistance with. the XML is generated from parsing a serialisable class:
[Serializable]
[XmlRoot("FAMILY")]
public class FamilyBlock
{
public string NAME { get; set; }
public int AGE { get; set; }
public DateTime? DOB { get; set; }
public FamilyBlock(string name, int age, DateTime? dob)
{
NAME = name;
AGE = age;
DOB = dob;
}
}
I attemped to resolve the problem with a list an object but i get the following (the addition of the object name - this i don't need).
<FAMILY>
<MEMBER>
<NAME>
<AGE>
<DATEOFBIRTH>
</MEMBER>
<MEMBER>
<NAME>
<AGE>
<DATEOFBIRTH>
</MEMBER>
<MEMBER>
<NAME>
<AGE>
<DATEOFBIRTH>
</MEMBER>
<MEMBER>
<NAME>
<AGE>
<DATEOFBIRTH>
</MEMBER>
</FAMILY>
i'm sure this is a simple problem but i really dont have much knowledge of xml
Upvotes: 1
Views: 389
Reputation: 6278
Additionally, i refactored it slightly. I created class named FamilyMember:
public class FamilyMember
{
public FamilyMember()
{
}
public FamilyMember(string name, string age, string dob)
{
NAME =name;
// etc etc
}
[XmlElement("NAME")]
public string NAME { get; set; }
[XmlElement("AGE")]
public string AGE { get; set; }
[XmlElement("DOB")]
public string DOB { get; set; }
}
}
so my XML class which i want to serialise now looks like:
[Serializable]
[XmlRoot("RESY")]
public class FamilyBlock: IXmlSerializable
{
public List<FamilyMember> FAMILYMEMBERS{ get; set; }
public FamilyBlock()
{
}
public FamilyBlock(string name, int age, DateTime? dob)
{
var familyMembers = new List<FamilyMember> // etc etc
....
}
public void WriteXml(XmlWriter writer)
{
foreach (var item in FAMILYMEMBERS)
{
writer.WriteElementString("NAME", item.NAME);
writer.WriteElementString("AGE", item.AGE);
writer.WriteElementString("DOB", item.DOB);
}
}
}
Upvotes: 0
Reputation: 6278
ok so what i did with the help from Nagg was to implement the IXmlSerializable interface.
[Serializable]
[XmlRoot("FAMILY")]
public class FamilyBlock : IXmlSerializable
{
[XmlElement("NAME")]
public List<string> NAME { get; set; }
[XmlElement("AGE")]
public List<int> AGE { get; set; }
[XmlElement("DOB")]
public List<DateTime?> DOB { get; set; }
public FamilyBlock(string name, int age, DateTime? dob)
{
NAME = name;
AGE = age;
DOB = dob;
}
public void WriteXml(XmlWriter writer)
{
for (int i = 0; i < this.NAME.Count; i++)
{
writer.WriteElementString("NAME ", this.NAME[i]);
writer.WriteElementString("AGE", this.AGE[i]);
writer.WriteElementString("DOB", this.DOB[i]);
}
}
}
if its not the correct approach, please feel free to update but this works for me. Please note, the following to methods are also inherited:
public XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
RESULT:
<FAMILY>
<NAME>
<AGE>
<DATEOFBIRTH>
<NAME>
<AGE>
<DATEOFBIRTH>
<NAME>
<AGE>
<DATEOFBIRTH>
<NAME>
<AGE>
<DATEOFBIRTH>
</FAMILY>
Upvotes: 0
Reputation: 6142
[XmlRoot("Family")]
public class FamilyBlock
{
[XmlElement("NAME")]
public string[] NAME { get; set; }
[XmlElement("AGE")]
public int[] AGE { get; set; }
[XmlElement("DOB")]
public DateTime?[] DOB { get; set; }
}
After xml serialization looks like:
<?xml version="1.0"?>
<Family xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NAME>a</NAME>
<NAME>s</NAME>
<AGE>1</AGE>
<AGE>3</AGE>
<DOB>2011-07-04T13:51:20.6757286+03:00</DOB>
<DOB xsi:nil="true" />
</Family>
Upvotes: 1