2012-03-21 2 views
0

XMLでxmlを使い慣れていません。xmlファイルで検索する必要があります。たとえば、私はこのXMLファイルを持っています。XMLで.netを使用してクエリする(ヘルプが必要です)

<?xml version="1.0" encoding="utf-8"?> 
<Books> 
    <Book> 
    <Title>Sample book1</Title> 
    </Book> 
    <Book> 
    <Title>Sample book2</Title> 
    </Book> 
    <Book> 
<Title>Another book</Title> 

、出力がサンプルBOOK1とBOOK2サンプルになりますので、例えば、私は、検索キーとして単語「サンプル」を使用。 "book"を検索キーとして使用すると、出力はSample book1、Sample book2、Another bookになります。

私が今持っているものからのベースは、それは不可能だと思われます。

  Dim xmlDocument As New XmlDocument() 
     Dim _xmlDataSource As New XmlDataSource 

     xmlDocument.Load(Server.MapPath("XML/Books.xml")) 

     Dim node = xmlDocument.DocumentElement.SelectSingleNode("./Books/Book[@title='SearchKey']") 

これは.netでも可能ですか?私のXMLファイルが要件に基づいて正しいフォーマットになっているかどうかを確認することもできます。 。billingData.Element( "ubrania")でdamskieから

するvarビリング=要素(:事前

答えて

1

に、私はこれは私の古いプロジェクトである(あなたがLINQの質問を使用することができます)あなたの一例を示すことができる

感謝"damska")要素( "pm")子孫( "item") 新しいdane_xml_panel((int)damskie.Element( "id"))を選択します。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.Xml.Linq; 

namespace sklep_internetowy_MVC.Models 
{ 
    public class pobierane_dane_xml 
    { 
     private List<dane_xml_panel> allBillings; 
     private XDocument billingData; 

     // constructor 
     public pobierane_dane_xml() 
     { 

       allBillings = new List<dane_xml_panel>(); 

       billingData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/dane.xml")); 
       var billings = from damskie in billingData.Element("ubrania").Element("damska").Element("pm").Descendants("item") 
           select new dane_xml_panel((int)damskie.Element("id")); 
       allBillings.AddRange(billings.ToList<dane_xml_panel>()); 

     } 

     // return a list of all billings 
     public IEnumerable<dane_xml_panel> GetBillings() 
     { 
       return allBillings; 
     } 


     public dane_xml_panel GetBillingByID(int id) 
     { 
       return allBillings.Find(item => item.ID == id); 
     } 

     // Insert Record 
     public void InsertBilling(dane_xml_panel billing) 
     { 
       billing.ID = (int)(from b in billingData.Descendants("item") orderby (int)b.Element("id") descending select (int)b.Element("id")).FirstOrDefault() + 1; 

       billingData.Root.Add(new XElement("item", new XElement("id", billing.ID))); 

       billingData.Save(HttpContext.Current.Server.MapPath("~/App_Data/dane.xml")); 
     } 

     // Delete Record 
     public void DeleteBilling(int id) 
     { 
       billingData.Root.Elements("item").Where(i => (int)i.Element("id") == id).Remove(); 

       billingData.Save(HttpContext.Current.Server.MapPath("~/App_Data/dane.xml")); 
     } 

     // Edit Record 
     public void EditBilling(dane_xml_panel billing) 
     { 
      XElement node = billingData.Root.Elements("item").Where(i => (int)i.Element("id") == billing.ID).FirstOrDefault(); 


      billingData.Save(HttpContext.Current.Server.MapPath("~/App_Data/dane.xml")); 
     } 
    } 
} 
クラス

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 


namespace sklep_internetowy_MVC.Models 
{ 
    public class dane_xml_panel 
    { 
      public dane_xml_panel() 
     { 
      this.ID = 0; 

     } 

      public dane_xml_panel(int id) 
      { 
       this.ID = id; 
      } 

     public int ID { get; set; } 

    } 
} 

、ここであなたが持っているxmlファイル(デーンXML):

<?xml version="1.0" encoding="utf-8"?> 
<ubrania> 
    <damska> 
     <nk id="Nowa Kolekcja"> 

      <item> 
        <id>1</id> 

      </item> 
      <item> 
        <id>2</id> 

      </item> 

     </nk> 
     <pm id="Nowosci"> 

      <item> 
        <id>3</id> 

      </item> 
      <item> 
        <id>4</id> 

      </item> 

     </pm> 

    </damska> 


</ubrania> 
関連する問題