Click here to Skip to main content
14,929,005 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am using xmlserialization, created some classes, but for one node I want xml as
XML
<AliasNames>
<AliasGivenName1>Joe</AliasGivenName1>
<AliasMiddleName1>A</AliasMiddleName1>
<AliasFamilyName1>Doe</AliasFamilyName1>
<AliasGivenName2>Jerry</AliasGivenName2>
<AliasMiddleName2>B</AliasMiddleName2>
<AliasFamilyName2>Doe</AliasFamilyName2>
<AliasGivenName3>Jim</AliasGivenName3>
<AliasMiddleName3>C</AliasMiddleName3>
<AliasFamilyName3>Doe</AliasFamilyName3>


</AliasNames>

This contains index added to xml node.

how can I accomplish this, please help.

Thanks in advance.
Posted
Updated 26-Jul-13 3:23am
v2
Comments
Pheonyx 26-Jul-13 9:22am
   
Sorry but your question is unclear, can you explain further.
Please use the Improve Question option to add more information to explain your requirements.

You should reconsider the way you are serialising your data to have something more "xml-like":
XML
<aliasnames>
  <aliasname>
    <given>Joe</given>
    <middle>A</middle>
    <family>Doe</family>
  </aliasname>
  <aliasname>
    <given>Jerry</given>
    <middle>B</middle>
    <family>Doe</family>
  </aliasname>
  <aliasname>
    <given>Jim</given>
    <middle>C</middle>
    <family>Doe</family>
  </aliasname>
</aliasnames>

This can be done using an array of AliasName class with Given, Middle and Family properties.

If you cannot change the xml format, you should use the XmlDocument by yourself:
C#
XmlDocument xml = new XmlDocument();
XmlElement eltRoot = xml.CreateElement("AliasNames");
xml.AppendChild(eltRoot);
for(int i=1; i<=arrayOfNames.Length(); ++i)
{
  XmlElement elt = xml.CreateElement("AliasGivenName"+i);
  elt.InnertText = arrayOfNames[i].GivenName;
  eltRoot.AppendChild(elt);
  elt = xml.CreateElement("AliasMiddleName"+i);
  elt.InnerText = arrayOfNames[i].MiddleName;
  eltRoot.AppendChild(elt);
  elt = xml.CreateElement("AliasFamilyName"+i);
  elt.InnertText = arrayOfNames[i].FamilyName;
  eltRoot.AppendChild(elt);
}
xml.Save(filename);
   
v2
Comments
pritam ajmire 29-Jul-13 4:37am
   
Thnak you for your reply,
But this is third party requirement that I want to request only in given format, and that's why I need to create xml like

<AliasNames>
<AliasGivenName1>Joe</AliasGivenName1>
<AliasMiddleName1>A</AliasMiddleName1>
<AliasFamilyName1>Doe</AliasFamilyName1>
<AliasGivenName2>Jerry</AliasGivenName2>
<AliasMiddleName2>B</AliasMiddleName2>
<AliasFamilyName2>Doe</AliasFamilyName2>
<AliasGivenName3>Jim</AliasGivenName3>
<AliasMiddleName3>C</AliasMiddleName3>
<AliasFamilyName3>Doe</AliasFamilyName3>


</AliasNames>

Please help...
Pascal-78 29-Jul-13 5:13am
   
I have updated my solution with a "manually" created XmlDocument
pritam ajmire 29-Jul-13 5:27am
   
Thank you for your response, how can I integrate this code with xml serializer , as xml that I need to create is very large & this is one on the middle part in it. All xml is created by xml-serializer. Or can I do [XmlArray] for aliasname & passing result of your code?? or any other way..??
Don't do it that way. Do this instead:
HTML
<aliasnames>
 <aliasname>
   <aliasgivenname>Joe</aliasgivenname>
   <aliasmiddlename>A</aliasmiddlename>
   <aliasfamilyname>Doe</aliasfamilyname>
 </aliasname>
 <aliasname>
   <aliasgivenname>Jerry</aliasgivenname>
   <aliasmiddlename>B</aliasmiddlename>
   <aliasfamilyname>Doe</aliasfamilyname>
 </aliasname>
 <aliasname>
   <aliasgivenname>Jim</aliasgivenname>
   <aliasmiddlename>C</aliasmiddlename>
   <aliasfamilyname>Doe</aliasfamilyname>
 </aliasname>
</aliasnames>
   
Thanks for your response. I have integrated code supplied by you with my code using xml serializer.
C#
// function returns XmlDocument with values
public XmlDocument GetAliasNames()
{
  XmlDocument xml = new XmlDocument();
  XmlElement eltRoot = xml.CreateElement("AliasNames");
  xml.AppendChild(eltRoot);
  for(int i=1; i<=arrayOfNames.Length(); ++i)
  {
    XmlElement elt = xml.CreateElement("AliasGivenName"+i);
    elt.InnertText = arrayOfNames[i].GivenName;
    eltRoot.AppendChild(elt);
    elt = xml.CreateElement("AliasMiddleName"+i);
    elt.InnerText = arrayOfNames[i].MiddleName;
    eltRoot.AppendChild(elt);
    elt = xml.CreateElement("AliasFamilyName"+i);
    elt.InnertText = arrayOfNames[i].FamilyName;
    eltRoot.AppendChild(elt);
  }
  return xml;
}


I had a class as;
C#
public class _PersonalData
{
    public _PersonName PersonName = new _PersonName();
    public _DemographicDetail DemographicDetail = new _DemographicDetail();

    public XmlDocument AliasNames { get; set; }
}



and I set values as;

C#
PersonalData.AliasNames = GetAliasNames();


and my xml is as;

XML
<BackgroundSearchPackage>
<PersonalData>
<PersonName>
<GivenName>pritam</GivenName>
<MiddleName>ravi</MiddleName>
<FamilyName>Ajmire</FamilyName>
</PersonName>
<DemographicDetail>
<Ssn>234523dsf</Ssn>
<DateOfBirth>14/23/1990</DateOfBirth>
<Race>dsf</Race>
<GenderCode>M</GenderCode>
</DemographicDetail>
<AliasNames>
<AliasNames>
<AliasGivenName1>GN1</AliasGivenName1>
<AliasMiddleName1>MN1</AliasMiddleName1>
<AliasFamilyName1>FN1</AliasFamilyName1>
</AliasNames>
</AliasNames>
</PersonalData>
</BackgroundSearchPackage>
   
v2
Comments
Pascal-78 29-Jul-13 7:26am
   
I am happy you find the solution to integrate this in an XmlSerializer.
I have learned something too: Serializing a property of type XmlDocument will include it in the final XmlDocument.
You may include the code to generate the XmlDocument and to read from it directly in the get and set accessors

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900