2012-03-14 139 views
0

我想解析使用LINQ到Xml的以下xml文檔,但我似乎無法得到任何輸出。使用Linq解析XML數據到Xml

<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> 

下面是我試圖解析XML數據

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 

      }; 

//的屬性列表綁定到數據網格 propertyGrid.ItemsSource = properties.ToList()的方式;

任何幫助將大大appeciated。

回答

3

(您select條款的開始是目前無效,但我會忽略...)

你不採取命名空間考慮:

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, 

注意呼叫properties.ToString不會給你任何有用的東西。你需要:

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

...甚至這是假設你Property類覆蓋ToString

編輯:短,但完整的程序來證明它的工作:

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); 
     } 
    } 
} 

示例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> 

輸出:

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

感謝你的幫助,我剛纔試過這種方法,但它通過我在我的xml文件 – 2012-03-14 20:41:17

+0

@FrancisTchatchoua的根元素的命名空間的方式仍然不工作:是的,但其中只有一個是* default *名稱空間。該代碼*應該*工作。我會寫一個簡短但完整的程序來展示它。 – 2012-03-14 20:41:59

+0

好的,先謝謝你,謝謝你的幫助 – 2012-03-14 20:45:30