简体   繁体   中英

Parsing xml data using Linq to Xml

I'm trying to parse the following xml document using LINQ to Xml but I can't seem to get any output.

 <ArrayOfCustomProperty xmlns="http://schemas.datacontract.org/2004/07/PropertySearchRestfulService.PropertySearchSoapService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <CustomProperty>
    <addressLine1Field>The Boulevard,</addressLine1Field>
    <addressLine2Field>Imperial Wharf</addressLine2Field>
    <addressLine3Field>Fulham</addressLine3Field>
    <descriptionField>This impressive penthouse apartment is arranged across two floors in the prestigious Chelsea Vista Development with numerous roof terraces with panoramic views across London. For viewing times, call to arrange your allocated appointment time.</descriptionField>
    <forRentOrSaleField>Sale </forRentOrSaleField>
    <furnitureField>Furnished</furnitureField>
    <gardenSizeField>0</gardenSizeField>
    <hasGardenField>false</hasGardenField>
    <numberOfBathroomsField>5</numberOfBathroomsField>
    <numberOfBedroomsField>4</numberOfBedroomsField>
    <postCodeField>SW6 5TG</postCodeField>
    <propertyImagesField xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <a:string>House1.jpg</a:string>
    </propertyImagesField>
    <propertyTypeField />
    <rentModeField />
    <rentPriceField>0</rentPriceField>
    <salePriceField>267000</salePriceField>
    <statusField>Available</statusField>
  </CustomProperty>

Below is the way i attempted to parse the xml data

properties = from property in xmlProperty.Descendants("CustomProperty")

            select new Property
            {

                ("propertyImagesField").Value;
                Description = property.Element("descriptionField").Value,

                PropertyType = property.Element("propertyTypeField").Value,
                AddressLine1 = property.Element("addressLine1Field").Value,
                AddressLine2 = property.Element("addressLine2Field").Value,
                AddressLine3 = property.Element("addressLine3Field").Value,
                PostCode = property.Element("postCodeField").Value,
                NumberOfBedrooms = property.Element("numberOfBedrsoomField").Value,
                NumberOfBathrooms = property.Element("numberOfBathroomsField").Value,
                Furniture = property.Element("furnitureField").Value,
                HasGarden = property.Element("hasGardenField").Value,
                GardenSize = property.Element("gardenSizeField").Value,
                ForRentOrSale = property.Element("forRentOrSaleField").Value,
                RentPrice = property.Element("rentPriceField").Value,
                RentMode = property.Element("rentModeField").Value,
                SalePrice = property.Element("salePriceField").Value,
                Status = property.Element("statusField").Value

            };

//bind the list of property to the datagrid propertyGrid.ItemsSource = properties.ToList();

Any help wil be greatly appeciated.

(The start of your select clause is invalid at the moment, but I'll ignore that...)

You're not taking the namespace into account:

XNamespace ns = "http://schemas.datacontract.org/2004/07/" + 
                "PropertySearchRestfulService.PropertySearchSoapService";

properties = from property in xmlProperty.Descendants(ns + "CustomProperty")
             ...
             // Later on, the same problem when selecting...
             Description = property.Element(ns + "descriptionField").Value,

Note that calling properties.ToString won't give you anything useful. You'll need:

foreach (var property in properties)
{
    MessageBox.Show(property.ToString());
}

... and even that's assuming that your Property class overrides ToString .

EDIT: Short but complete program to demonstrate it working:

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load("test.xml");
        XNamespace ns = "http://test-namespace1";

        var query =
              from element in doc.Descendants(ns + "CustomProperty")
              select new {
                 Description = element.Element(ns + "descriptionField").Value,
                 Furniture = element.Element(ns + "furnitureField").Value
              };

        foreach (var record in query)
        {
            Console.WriteLine(record);
        }
    }
}

Sample XML:

<ArrayOfCustomProperty xmlns="http://test-namespace1" 
                       xmlns:i="http://test-namespace2">
  <CustomProperty>
    <descriptionField>First house</descriptionField>
    <furnitureField>Furnished</furnitureField>
  </CustomProperty>
  <CustomProperty>
    <descriptionField>Second house</descriptionField>
    <furnitureField>Unfurnished</furnitureField>
  </CustomProperty>
</ArrayOfCustomProperty>

Output:

{ Description = First house, Furniture = Furnished }
{ Description = Second house, Furniture = Unfurnished }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM