0

I cannot bind my c# model with this kind of xml file. Because there is multiple element with same. This is a example of my XML File.

  <Type>
       <Loop LoopId="2100A" Name="MEMBER NAME">
         <PER>
           <!--Contact Function Code-->
           <PER01>IP<!--Insured Party--></PER01>
           <PER02 />
           <!--Communication Number Qualifier-->
           <PER03>HP<!--Home Phone Number--></PER03>
           <!--Communication Number-->
           <PER04>6235834409</PER04>
         </PER>
       </Loop>
       <Loop LoopId="2100C" Name="MEMBER MAILING ADDRESS">
         <NM1>
           <!--Entity Identifier Code-->
           <NM101>31<!--Postal Mailing Address--></NM101>
           <!--Entity Type Qualifier-->
           <NM102>1<!--Person--></NM102>
         </NM1>
       </Loop>
       <Loop LoopId="2100G" Name="RESPONSIBLE PERSON">
         <PER>
           <!--Contact Function Code-->
           <PER01>IP<!--Insured Party--></PER01>
           <PER02 />
           <!--Communication Number Qualifier-->
           <PER03>HP<!--Home Phone Number--></PER03>
           <!--Communication Number-->
           <PER04>6235834409</PER04>
         </PER>
         <LM>
           <!--Contact Function Code-->
           <LM01>RP<!--Responsible Person--></LM01>
           <LM02 />
         </LM>
       </Loop>
   </Type>

I have use this following code to bind. But Code is not working, cause data binding get confused.

        [XmlElement(ElementName = "Loop")]
        public L3_L1_MemberName L3_L1_MemberName { get; set; }

        [XmlElement(ElementName = "Loop")]
        public L3_L2_MemberMailingAddress L3_L2_MemberMailingAddress { get; set; }

        [XmlElement(ElementName = "Loop")]
        public L3_L3_ResponsiblePerson L3_L3_ResponsiblePerson { get; set; }

1 Answer 1

2

based your XML, the model looks like

[XmlRoot(ElementName = "PER")]
public class PER
{
    [XmlElement(ElementName = "PER01")]
    public string PER01 { get; set; }
    [XmlElement(ElementName = "PER02")]
    public string PER02 { get; set; }
    [XmlElement(ElementName = "PER03")]
    public string PER03 { get; set; }
    [XmlElement(ElementName = "PER04")]
    public string PER04 { get; set; }
}

[XmlRoot(ElementName = "Loop")]
public class Loop
{
    [XmlElement(ElementName = "PER")]
    public PER PER { get; set; }
    [XmlAttribute(AttributeName = "LoopId")]
    public string LoopId { get; set; }
    [XmlAttribute(AttributeName = "Name")]
    public string Name { get; set; }
    [XmlElement(ElementName = "NM1")]
    public NM1 NM1 { get; set; }
    [XmlElement(ElementName = "LM")]
    public LM LM { get; set; }
}

[XmlRoot(ElementName = "NM1")]
public class NM1
{
    [XmlElement(ElementName = "NM101")]
    public string NM101 { get; set; }
    [XmlElement(ElementName = "NM102")]
    public string NM102 { get; set; }
}

[XmlRoot(ElementName = "LM")]
public class LM
{
    [XmlElement(ElementName = "LM01")]
    public string LM01 { get; set; }
    [XmlElement(ElementName = "LM02")]
    public string LM02 { get; set; }
}

[XmlRoot(ElementName = "Type")]
public class Type
{
    [XmlElement(ElementName = "Loop")]
    public List<Loop> Loop { get; set; }
}

Here is the logic to DeSerialize the XML to Object

XmlSerializer serializer = new XmlSerializer(typeof(Type));
string xml = File.ReadAllText("XMLFile3.xml");
using (TextReader reader = new StringReader(xml))
{
    var results = (Type)serializer.Deserialize(reader);
    foreach (var item in results.Loop)
    {
        Console.WriteLine($"{item.LoopId} {item.Name}");
        if (item.PER != null)
        {
            Console.WriteLine($"PER:{Regex.Replace(item.PER.PER01, @"\s+", "")}-{Regex.Replace(item.PER.PER02, @"\s+", "")}-{Regex.Replace(item.PER.PER03, @"\s+", "")}-{Regex.Replace(item.PER.PER04, @"\s+", "")}");
        }                      

        if (item.NM1 != null)
        {
            Console.WriteLine($"NM1:{Regex.Replace(item.NM1.NM101, @"\s+", "")}-{Regex.Replace(item.NM1.NM102, @"\s+", "")}");
        }

        if (item.LM != null)
        {
            Console.WriteLine($"LM:{Regex.Replace(item.LM.LM01, @"\s+", "")}-{Regex.Replace(item.LM.LM02, @"\s+", "")}");
        }
    }
}

OUTPUT

2100A MEMBER NAME
PER:IP--HP-6235834409
2100C MEMBER MAILING ADDRESS
NM1:31-1
2100G RESPONSIBLE PERSON
PER:IP--HP-6235834409
LM:RP-
Sign up to request clarification or add additional context in comments.

4 Comments

@akb, the problem is with XML serialization?
No.. XML Deserialization
3 loops are containing different elements. That is it's getting Confused.
@akb, have modified as per new XML. You can mark this as an Answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.