How to get the return value using LINQ from an XML file

0

I have an application which calls a web service. For this I am using LINQ.

What I want to do is the value of 'Caption' come from an XML file in which I pass as a parameter.

When doing the debug I see that the parameter passes well, but it returns null.

What am I doing wrong?

Here is my code:

This is the XML file:

<root>   
     <dataSourceCaption>Users</dataSourceCaption>
</root>

This is the code:

int count = xml.GetElementsByTagName("dataSourceCaption").Count;

for (int i = 0; i < count; ++i)
{
    dataSourceCaption.Add(xml.GetElementsByTagName("dataSourceCaption")[i].InnerText);
}

var ds = etDataSourceMappingTable();
foreach (DataTable dst in ds.Tables)
{
    foreach (DataRow dr in dst.Rows)
    {
        //var DataSourceId = dr["SourceID"].ToString();
        var DataSourceId = ds.Tables["Table"]
            //.Select("Caption = 'TeachersTests'") // Find rows with Caption
        .Select("Caption = '" + dataSourceCaption.ToString() + "'") // Find rows with Caption
        .Select(r => r["SourceID"])       // Project to the value of SourceId
        .Where(s => s != DBNull.Value)    // Filter DBNull (might occur when the SourceID cell is missing
        .Select(s => s.ToString())       // Project to string value
        .FirstOrDefault();

        SourceID = DataSourceId;
    }
}
return SourceID;

This is the schema of the web service I'm calling:

  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
       <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
          <xs:complexType>
             <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="Table">
                   <xs:complexType>
                      <xs:sequence>
                         <xs:element name="SourceID" type="xs:string" minOccurs="0"/>
                         <xs:element name="Caption" type="xs:string" minOccurs="0"/>
                      </xs:sequence>
                   </xs:complexType>
                </xs:element>
             </xs:choice>
          </xs:complexType>
       </xs:element>
    </xs:schema>
    
asked by A arancibia 18.11.2016 в 16:34
source

1 answer

0

You could do something like this:

XmlDocument xDoc = new XmlDocument();
                xDoc.Load(@"\..\..\ComprobantePagos.xml");
                XmlNodeList xTipoComprobante = xDoc.GetElementsByTagName("DocumentElement");
                XmlNodeList xLista = ((XmlElement)xTipoComprobante[0]).GetElementsByTagName("TipoComprobante");

                foreach (XmlElement nodo in xLista)
                {
                    int i = 0;
                    XmlNodeList xNumero = nodo.GetElementsByTagName("Numero");
                    XmlNodeList xDescripcion = nodo.GetElementsByTagName("Descripcion");

                    TipoComprobante entity = new TipoComprobante
                    {
                        Numero = xNumero[i].InnerText,
                        Descripcion = xDescripcion[i++].InnerText
                    };

It is an example code, you should adapt it to your needs.

    
answered by 18.11.2016 / 18:10
source