0

i trying get all array records from Xml using Linq but it gets error object null and i successfully fetched the single array but i cant able to fetch more than one array like XML file

c# File

        var xml = XDocument.Load("C:\\Applications\\tidenew\\TideTest\\TideTest\\Files\\" + FileName);
        var format = from data in xml.Descendants("Bordereau")
                     select new
                     {

                         //Step 7
                         CoreFinancialList = data.Element("Financial").Elements("CoreFinancial")
                                            .Select(s => new
                                            {
                                                FieldTypeEnum = s.Element("FieldTypeEnum").Value,
                                                IsGpBasedRiskOrder = s.Element("IsGpBasedRiskOrder").Value,
                                                IsMultiSelect = s.Element("IsMultiSelect").Value,
                                                LableText = s.Element("LableText").Value,
                                                MappingOptions = s.Element("MappingOptions").Value,
                                                MappingOptionsColumns = s.Elements("MappingOptionsColumns")
                                                                                .Elements("value")
                                                                                .Select(x => x.Value)
                                                                                .ToArray(),

                                                MappingType = s.Element("MappingType").Value,
                                                SchemaField = s.Element("SchemaField").Value,
                                                Value = s.Element("Value").Value,
                                                ErrCount = s.Element("ErrCount").Value,
                                                valueError = s.Element("valueError").Value,
                                            }).ToList(),

                     };

this is my code to fetch array of records from XML

XML File

  <Financial>
    <CoreFinancial>
      <FieldTypeEnum>0</FieldTypeEnum>
      <IsGpBasedRiskOrder>true</IsGpBasedRiskOrder>
      <IsMultiSelect>false</IsMultiSelect>
      <LableText>Risk Currency</LableText>
      <MappingOptions></MappingOptions>
      <MappingOptionsColumns></MappingOptionsColumns>
      <MappingType>3</MappingType>
      <SchemaField>RiskCurrrency</SchemaField>
      <Value>GBP</Value>
      <ErrCount>0</ErrCount>
      <valueError>false</valueError>
    </CoreFinancial>
    <CoreFinancial>
      <FieldTypeEnum>1</FieldTypeEnum>
      <IsGpBasedRiskOrder>true</IsGpBasedRiskOrder>
      <IsMultiSelect>false</IsMultiSelect>
      <LableText>Premium Currenc</LableText>
      <MappingOptions></MappingOptions>
      <MappingOptionsColumns></MappingOptionsColumns>
      <MappingType>3</MappingType>
      <SchemaField>PremiumCurrency</SchemaField>
      <Value>GBP</Value>
      <ErrCount>0</ErrCount>
      <valueError>false</valueError>
    </CoreFinancial>
    <CoreFinancial>
      <FieldTypeEnum>3</FieldTypeEnum>
      <IsGpBasedRiskOrder>true</IsGpBasedRiskOrder>
      <IsMultiSelect>true</IsMultiSelect>
      <LableText>Gross Premium</LableText>
      <MappingOptions></MappingOptions>
      <MappingOptionsColumns type="Array">
        <value>Gross Premium</value>
        <value>Address 1</value>
      </MappingOptionsColumns>
      <MappingType>5</MappingType>
      <SchemaField>GrossPremium</SchemaField>
      <Value></Value>
      <ErrCount>0</ErrCount>
      <valueError>false</valueError>
    </CoreFinancial>

  </Financial>

Error image

click here

Siva Gopal
  • 3,474
  • 1
  • 25
  • 22
user6254141
  • 21
  • 1
  • 6
  • What does 'error object null` mean? Do you mean an exception? If so, please including exception.ToString(). Also, show us the line of code on which the exception occurs. – mjwills Jun 28 '17 at 11:53
  • @user6254141 If your code working on exact XML you pasted here with `....` your code is working fine and I can see the results also. I guess are you doing something wrong inside the `foreach` loop? – Siva Gopal Jun 28 '17 at 12:38
  • See where the popup says “Copy Details”? Click that and paste the results into your question. – Dour High Arch Jun 29 '17 at 00:33

1 Answers1

2

.Net have another method of deserialization xml's

1) Generate structure of your xml by using xsd or you can use online tool xml to C#

For xsd method* Generate the xsd using cmd:

xsd foo.xml

xsd foo.xsd /classes

2) Bring your xml class to project

public class CoreFinancial {
    public string FieldTypeEnum { get; set; }
    public string IsGpBasedRiskOrder { get; set; }
    public string IsMultiSelect { get; set; }
    public string LableText { get; set; }
    public string MappingOptions { get; set; }
    public string MappingType { get; set; }
    public string SchemaField { get; set; }
    public string Value { get; set; }
    public string ErrCount { get; set; }
    public string ValueError { get; set; }
    public MappingOptionsColumns MappingOptionsColumns { get; set; }
}

public class MappingOptionsColumns {
    [XmlElement(ElementName="value")]
    public List<string> Value { get; set; }
    [XmlAttribute(AttributeName="type")]
    public string Type { get; set; }
}

public class Financial {
    public List<CoreFinancial> CoreFinancial { get; set; }
}

3) Use XmlSerializer for deserialization

XmlSerializer ser = new XmlSerializer(typeof(Financial));
Financial finlist;
using (XmlReader reader = XmlReader.Create(path)) //Path to your xml with filename
{
    finlist = (Financial) ser.Deserialize(reader);
}
foreach (var item in finlist) {
            //use item and do whatever your wants
        }
itikhomi
  • 1,560
  • 13
  • 13