2012-03-14 26 views
0

LINQ to XMLを使用して次のXML文書を解析しようとしていますが、出力が得られないようです。以下はXMLにLinqを使用して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> 
は、i)は

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にプロパティのリストをバインドするXMLデータを解析しようとした方法です。

大いに助けてください。

答えて

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をオーバーライドすることを想定しています:あなたが必要となります。

EDITは:

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

あなたの助けをありがとう、私はこの方法を試してみましたが、それはまだdoesntの仕事私は私のxmlファイルのルート要素 –

+0

@FrancisTchatchouaで2つの名前空間を持っている方法によって:はい、ただし、それらのうちの1つのみが* default *名前空間です。そのコード*は機能するはずです。私はそれを示す短いしかし完全なプログラムを書くつもりです。 –

+0

よろしくお願いします。ありがとうございました。ありがとうございました –

関連する問題