0

I have this xml data

<Records>
      <Person id="1234" action="chg" date="12-Oct-2000">
         <Gender>Male</Gender>
         <ActiveStatus>Active</ActiveStatus>
         <Deceased>No</Deceased>
         <NameDetails>
            <Name NameType="Primary Name">
               <NameValue>
                  <FirstName>Joe</FirstName>
                  <Surname>Doe</Surname>
                  <OriginalScriptName>Joe Doe</OriginalScriptName>
               </NameValue>
            </Name>
            <Name NameType="Spelling Variation">
               <NameValue>
                  <FirstName>John</FirstName>
                  <Surname>Doo</Surname>
               </NameValue>
               <NameValue>
                  <FirstName>Joeh</FirstName>
                  <Surname>Doeh</Surname>
               </NameValue>
               <NameValue>
                  <FirstName>Jay</FirstName>
                  <Surname>Doe</Surname>
               </NameValue>
               <NameValue>
                  <FirstName>Joo</FirstName>
                  <Surname>Doe</Surname>
               </NameValue>
            </Name>         
        </NameDetails>
    </Person>
</Records>  

I can select the value for Gender, ActiveStatus and Deceased with this code:

XmlNodeList xmlNodePersonList = xmlDocument.SelectNodes("/Records/Person");

var personProfileList = new List<PersonProfile>();

foreach (XmlNode childEllement in xmlNodePersonList)
{
    var personProfile = new PersonProfile
    {
        Gender = childEllement["Gender"].InnerText,
        ActiveStatus = childEllement["ActiveStatus"].InnerText,
        Deceased = childEllement["Deceased"].InnerText
    };
    personProfileList.Add(personProfile);
}

But how do I select the value for NameDetails for that structure?

1
  • Is xpath an imperative or you can use Linq to XML ? Commented Oct 23, 2020 at 8:17

1 Answer 1

1

It's as simple as pasting the XML into Visual Studio and choosing "paste specials", it will create all your classes. Then deserialize, it couldn't be simpler really:

var serializer = new XmlSerializer(typeof(Records));
using TextReader reader = new StreamReader(new FileStream(@"D:\funky.xml", FileMode.Open));

// All your data is there in result
var result = (Records) serializer.Deserialize(reader);

Given

public class Records
{
   public RecordsPerson Person { get; set; }
}

public class RecordsPerson
{
   public string Gender { get; set; }
   public string ActiveStatus { get; set; }
   public string Deceased { get; set; }
   public RecordsPersonName[] NameDetails { get; set; }    
   public ushort id { get; set; }  
   public string action { get; set; }
   public string date { get; set; }
}

public class RecordsPersonName
{
   public RecordsPersonNameNameValue[] NameValue { get; set; } 
   public string NameType { get; set; }
}

public class RecordsPersonNameNameValue
{
   public string FirstName { get; set; }
   public string Surname { get; set; }
   public string OriginalScriptName { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Actually I do have the model already. Now it's to read the xml value from xml file and then copied to the model
@Steve var result = (Records) serializer.Deserialize(reader); done
Is there a way to auto generate all the DbSet? I am using Entity Framework.

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.