2

Can someone please tell me how to run foreach cycle that goes through every element Person

I have this code for loading but doc1 is not filled with data

  XDocument doc = XDocument.Load(path);
  foreach (var doc1 in doc.Descendants("Person"))

The XML looks like this

<Report xmlns="http://askmk/ask/Report">
    <ReportTypeId>5</ReportTypeId>
    <BankId>111</BankId>
    <ReferenceNo>1</ReferenceNo>
    <ReferenceNoReporter />
    <DateCreated>2017-01-31T01:50:44.0000000+01:00</DateCreated>
    <DataFromDate>2017-01-27T12:00:00.0000000+01:00</DataFromDate>
    <DataToDate>2017-01-27T12:00:00.0000000+01:00</DataToDate>
    <PersonList>
<Person xmlns="http://askmk/ask/ReportTypes">
        <PersonObjectId>111</PersonObjectId>
        <CellPhoneNo>111      </CellPhoneNo>
        <DateOfBirth>1985-03-18</DateOfBirth>
        <Email />
        <EMBG>111111</EMBG>
        <IsResident>1</IsResident>
        <FirstName>xxx</FirstName>
        <GenderTypeId>3</GenderTypeId>
        <LastName>xxx</LastName>
        <PhoneNo />
        <PlaceOfBirth />
        <IdDocumentList>
          <IdDocument>
            <IdDocumentTypeId>1</IdDocumentTypeId>
            <PlaceOfIssue>.                                       </PlaceOfIssue>
            <IdNo>1111</IdNo>
          </IdDocument>
        </IdDocumentList>
      </Person>
<Person xmlns="http://askmk/ask/ReportTypes">
        <PersonObjectId>1111</PersonObjectId>
        <CellPhoneNo>11111      </CellPhoneNo>
        <DateOfBirth>1969-03-28</DateOfBirth>
        <Email />
        <EMBG>1111</EMBG>
        <IsResident>1</IsResident>
        <FirstName>xxx</FirstName>
        <GenderTypeId>3</GenderTypeId>
        <LastName>xxx</LastName>
        <PhoneNo />
        <PlaceOfBirth />
        <IdDocumentList>
          <IdDocument>
            <IdDocumentTypeId>2</IdDocumentTypeId>
            <PlaceOfIssue>xxxx                     </PlaceOfIssue>
            <IdNo>1111</IdNo>
          </IdDocument>
        </IdDocumentList>
      </Person>
    </PersonList>
</Report>

I know that this is simple but i am new to this c# and thats why i am asking.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95

2 Answers2

3

The problem is that you forgot the namespace:

XDocument doc = XDocument.Load(path);
XNamespace ns = "http://askmk/ask/ReportTypes";
foreach (var doc1 in doc.Descendants(ns + "Person"))
{
    //TODO
}

For more you can have a look at:


As @Alexander pointed out the + is the XNamespace.Addition operator.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • 1
    @SeM - it is fine using it :) The problem of doing so is when it is concatenation in a loop – Gilad Green Sep 12 '17 at 09:05
  • @SeM: The argument against string concatenation really only flies once you're concatenating three pieces (and arguably even more than that). For concatenating two pieces, there's no real excessive overhead by using +. – Flater Sep 12 '17 at 09:07
  • 2
    @SeM - it's not string concatenation. It's [XNamespace.Addition](https://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.op_addition(v=vs.110).aspx) operator. – Alexander Petrov Sep 12 '17 at 09:07
  • I was going to ask how this works without some sort of separator character or something between namespace and the local name (otherwise it looks like `ReportTypesPerson`) but @AlexanderPetrov seems to answer that with the addition operator. I looked to be sure: there is an implicit conversion from `string` to `XNamespace` as well which makes that actually work https://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.op_implicit(v=vs.110).aspx – pinkfloydx33 Sep 12 '17 at 09:12
  • @Сашко Мицевски - Glad it helped :) – Gilad Green Sep 12 '17 at 09:29
1

You can deserialize the xml instead to get the object of type Report which contains an IEnumerable of Person object. then you can iterate over the IEnumerable of Person .

You can get the an object of type Report by copying the xml in your clipboard, go to visual studio=> edit=> paste spacial=> paste xml as class.

This will create a the class for you.

class Program {

    static void Main(string[] args)
    {
         var path = "path to xml" or stream which contains your xml.


        XmlSerializer xs = new XmlSerializer(typeof(Report));


        using (StreamReader rd = new StreamReader(path))
        {
            var result = (Report)xs.Deserialize(rd);
            foreach(var p in result.Person)
                    { //TODO
                   }
        }



            Console.ReadLine();


    }
}
codein
  • 317
  • 2
  • 12