I have an XML file which has many children:
<Docs>
<Doc>
<DocType>x</DocType>
<DocNumber>xxx</DocNumber>
<DocNumberSeries>xx</DocNumberSeries>
<DocDate>xxxx-xx-xx</DocDate>
<DocCause>
<Id>xx</Id>
<Code/>
<Name>xx</Name>
</DocCause>
<Anag>
<Name>NameCompany</Name>
<Address>xx</Address>
<ZipCode>xx</ZipCode>
<City>xx</City>
<Province>xx</Province>
<CountryCode>xx</CountryCode>
<PhoneNumber>xx</PhoneNumber>
<CellularNumber/>
<FaxNumber>xx</FaxNumber>
<EmailAddress>xx</EmailAddress>
<VatNumber>xx</VatNumber>
<PersonalID>xx</PersonalID>
</Anag>
<DestinationAddress>
<Name>xxL</Name>
<Address>xx</Address>
<ZipCode>xx</ZipCode>
<City>xx</City>
<Province>xx</Province>
<CountryCode>xx</CountryCode>
<PhoneNumber>xx</PhoneNumber>
<FaxNumber>xx</FaxNumber>
</DestinationAddress>
<Payment>
<Code/>
<Name>xx</Name>
</Payment>
<DocRows>
<DocRow>
<RowType>1</RowType>
<Product>
<Code>LSML1710</Code>
</Product>
<Description></Description>
<Quantity></Quantity>
<Price></Price>
<Tax>
<Tax>
<Id>xx</Id>
<Code/>
<Name>xx</Name>
<PercentAmount>xx</PercentAmount>
<IndPercentAmount>x</IndPercentAmount>
<FixedAmount>x</FixedAmount>
</Tax>
</Tax>
</DocRow>
</DocRows>
...
</Doc>
For example, one of the thing I want to do is to get the data inside tag Doc
and then Anag
.
At the moment I have these classes:
public class Anag
{
public string RagioneSociale { get; set; }
public string Indirizzo { get; set; }
public string CAP { get; set; }
public string Citta { get; set; }
public string Provincia { get; set; }
public string CodiceNazione { get; set; }
public string Telefono { get; set; }
public string Cellulare { get; set; }
public string Email { get; set; }
}
public class Doc
{
public string DocNumber { get; set; }
public Anag Anags { get; set; }
}
List<Doc> results = contentFile.Descendants("Doc").Select(doc => new Doc // var results = contentFile.Descendants("Doc").SelectMany(doc => doc.Descendants(doc.Name.Namespace + "Anag").Select(anag => new
{
DocNumber = (string)doc.Element(doc.Name.Namespace + "DocNumber"),
Anags = doc.Descendants(doc.Name.Namespace + "Anag").Select(anag => new Anag
{
RagioneSociale = (string)anag.Element(anag.Name.Namespace + "Name"),
Indirizzo = (string)anag.Element(anag.Name.Namespace + "Address"),
CAP = (string)anag.Element(anag.Name.Namespace + "ZipCode"),
Provincia = (string)anag.Element(anag.Name.Namespace + "Province"),
Telefono = (string)anag.Element(anag.Name.Namespace + "PhoneNumber"),
Cellulare = (string)anag.Element(anag.Name.Namespace + "CellularNumber"),
Email = (string)anag.Element(anag.Name.Namespace + "EmailAddress"),
Citta = (string)anag.Element(anag.Name.Namespace + "City")
}),
}).ToList();
This last code gives an error:
Cannot implicitly convert type
system.collections.generic.IEnumerable<nameProject.Classes.Anag>
in<NameProject.Classes.Anag>
.
The final result I want is a List with the data and children of this XML:
List:
+ Doc=>
{DocNumber}
+ Anag => { Address .. , Name.. .. ) (not a list ! )
+ DestinationAddress => { Address.... Name..)
so that I can convert to a file .csv only .