Mam te dane XML

<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>  

Mogę wybrać wartość dla Gender, ActiveStatus i Deceased za pomocą tego kodu:

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);
}

Ale jak wybrać wartość dla NameDetails dla tej struktury?

0
Steve 23 październik 2020, 11:03

1 odpowiedź

Najlepsza odpowiedź

Jest tak proste jak wklejanie XML do Visual Studio i wybierając "Paste Specials", stworzy wszystkie twoje zajęcia. Potem Deserialize, naprawdę nie może być prostsze:

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);

podane

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; }
}
1
halfer 24 październik 2020, 20:41