Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How can I read the following XML file using LINQ


HTML
<Controls version="1.0">
  <ContentsTypes>
    <ContentType Id="DOCUMENT_SOC" Value="Document société">
      <SecretsLevels>
        <SecretLevel label="Confidentiel">100</SecretLevel>
        <SecretLevel label ="Public">500</SecretLevel>
      </SecretsLevels>
        <Index>
          <Control>
            <Id>1</Id>
            <Code>NOM</Code>
            <Type>TextBox</Type>
            <Mandatory>true</Mandatory>
            <Label>Nom société</Label>
            <Values></Values>
            <DefaultValue></DefaultValue>
          </Control>
          <Control>
            <Id>2</Id>
            <Code>AL_TYPE_SOC</Code>
            <Type>ComboBox</Type>
            <Mandatory>true</Mandatory>
            <Label>Type société<Label>
            <Values>
              <Value Id="COR">Correspondance</Value>
              <Value Id="BAN">Banque</Value>
              <Value Id="FIN">Finance</Value>
              <Value Id="COM">Comptes</Value>
              <Value Id="PRE">Prêt</Value>
            </Values>
            <DefaultValue></DefaultValue>
          </Control>
          <Control>
            <Id>3</Id>
            <Code>SOUS_DOS</Code>
            <Type>TextBox</Type>
            <Mandatory>false</Mandatory>
            <Label>Sous dossier</Label>
            <Values></Values>
            <DefaultValue></DefaultValue>
          </Control>
        </Index>
    </ContentType>
    <ContentType Id="DOCUMENT_FRN" Value="Document RH">
      <SecretsLevels>
        <SecretLevel label="Confidentiel RH">600</SecretLevel>
      </SecretsLevels>
        <Index>
          <Control>
            <Id>1</Id>
            <Code>NOM</Code>
            <Type>TextBox</Type>
            <Mandatory>true</Mandatory>
            <Label>Nom collaborateur</Label>
            <Values></Values>
            <DefaultValue></DefaultValue>
          </Control>
          <Control>
            <Id>2</Id>
            <Code>AL_TYPE_DOC</Code>
            <Type>ComboBox</Type>
            <Mandatory>true</Mandatory>
            <Label>Type document</Label>
            <Values>
              <Value Id="CV">CV</Value>
              <Value Id="FP">Fiche de paie</Value>
              <Value Id="DIV">Divers</Value>
            </Values>
            <DefaultValue></DefaultValue>
          </Control>
        </Index>
      </ContentType>
  </ContentsTypes>
</Controls> 


I try to use something like this, but don't work
C#
var query = from device in loaded.Descendants("ContentType")
                select new
                {
                    ContTypeId    = device.Attribute("Id").Value,
                    ContTypeValue = device.Attribute("Value").Value,
                    SecretsLvl = from deviceSecrets in device.Descendants("SecretsLevels")
                                 select new
                                 {
                                     SecretsLevel_Label = deviceSecrets.Attribute("label").Value,
                                     SecretsLevel_Value = deviceSecrets.Attribute("Value").Value,
                                     Index = from deviceIndex in deviceSecrets.Descendants("Index/Control")
                                             let valuesDig = deviceIndex.Element("Values").Elements("Value")
                                             let valuesAtt = deviceIndex.Element("Values").Elements("Value").Attributes("Id")
                                             select new 
                                             {
                                                 AttribDig = from attrDig in valuesAtt select attrDig.Value,
                                                 IdDig     = deviceIndex.Element("Id").Value,
                                                 CodeDig   = deviceIndex.Element("Code").Value,
                                                 TypeDig   = deviceIndex.Element("Type").Value,
                                                 LabelDig  = deviceIndex.Element("Label").Value,
                                                 MandaDig  = deviceIndex.Element("Mandatory").Value,
                                                 DefaultValueDig = deviceIndex.Element("DefaultValue").Value,
                                             }
                                 }
                };


Thanks very much
Posted
Updated 28-Feb-12 12:54pm
v2
Comments
R. Giskard Reventlov 28-Feb-12 17:35pm    
What was the error?
wizardzz 28-Feb-12 17:39pm    
Are you missing a 'from' before your most inner nested 'select new' ?

let valuesAtt = deviceIndex.Element("Values").Elements("Value").Attributes("Id")
select new
Orcun Iyigun 28-Feb-12 18:55pm    
Added tags.

You need to use the class System.Xml.Linq.XDocument designed with LINQ to XML in mind. Please see:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx[^].

In the section "Examples" on the page referenced above, you will find a LINQ sample.

To learn LINQ to XML, start here:
http://msdn.microsoft.com/en-us/library/bb387098.aspx[^].

—SA
 
Share this answer
 
v2
Thanks all for your suggestions, here is the solution:

List<ContentType> contentTypes = (from _contType in loaded.Descendants("ContentType")

select new ContentType
{
contentTypeId = _contType.Attribute("Id").Value,
contentTypeValue = _contType.Attribute("Value").Value,
secretlevels = (from _secretLevel in _contType.Descendants("SecretLevel")
select new SecretLevel {
secretLevelLabel = _secretLevel.Value,
secretLevelValue = _secretLevel.Attribute("label").Value,
}).ToList(),

controlsDigitech = (from ctrl in _contType.Descendants("Control")
select new ControlDig
{
id = Convert.ToInt32(ctrl.Element("Id").Value),
code = ctrl.Element("Code").Value,
type = ctrl.Element("Type").Value,
label = ctrl.Element("Label").Value,
mandatory = Convert.ToBoolean(ctrl.Element("Mandatory").Value),
defaultValue = ctrl.Element("DefaultValue").Value,
valuesDigitech = (from va in ctrl.Descendants("Value")
select new ValuesForDigtech
{
Value_d = va.Value,
Id_d = va.Attribute("Id").Value,
}).ToList()
}).ToList()
}).ToList();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900