2013-11-15 52 views
1

這是我的XML文件的一部分,並且我在我的c#窗體上有一個組合框,其中包含我放置的xml文件的設備名稱通過使用xpath導航器,再加上一個數字,最後是一個叫做Buy的按鈕。 我想要做的是當我點擊按鈕購買我想要的設備節點的QUANTITY節點值誰的名稱節點值等於組合框SelectedValue增加的數字向上值的數量。 換句話說,我怎樣才能選擇一個DEVICE元素的QUANTITY節點,該元素的NAME等於組合框中的名稱,然後使用C#向上增加數值。在C#中使用Xpath比較comboBox.Slectedvalue與XML文件節點值

<INVENTORY> 
<DEVICE ID="1"> 
    <NAME>Air Steerable Bagless Upright</NAME> 
    <BRAND>Hoover</BRAND> 
    <MODEL>UH72400</MODEL> 
    <QUANTITY>23</QUANTITY> 
    <BUYING_PRICE>189.99</BUYING_PRICE> 
    <SELLING_PRICE>229.99</SELLING_PRICE> 
</DEVICE> 
<DEVICE ID="2"> 
    <NAME>Quietforce Bagged Canister</NAME> 
    <BRAND>Hoover</BRAND> 
    <MODEL>SH30050</MODEL> 
    <QUANTITY>18</QUANTITY> 
    <BUYING_PRICE>299.99</BUYING_PRICE> 
    <SELLING_PRICE>334.99</SELLING_PRICE> 
</DEVICE> 
<DEVICE ID="3"> 
    <NAME>Corded Cyclonic Stick Vacuum</NAME> 
    <BRAND>Hoover</BRAND> 
    <MODEL>SH20030</MODEL> 
    <QUANTITY>21</QUANTITY> 
    <BUYING_PRICE>79.99</BUYING_PRICE> 
    <SELLING_PRICE>109.99</SELLING_PRICE> 
</DEVICE> 
+0

加載到自定義類型的XML文檔的內容?或者你只是在檢索名字? – ChrisK

+0

我將XML文檔加載到如下形式: XmlDocument inventory = new XmlDocument(); inventory.Load(「Inventory.xml」); 對不起,但我有點新編程,但我試圖趕上。 – Xavier1819

回答

0
private void button2_Click(object sender, EventArgs e)//Button Buy clicking method 
     { 
      XmlDocument inventory = new XmlDocument(); 
      inventory.Load("Inventory.xml"); 
      string vacuumName = (string)vacuumsBox.SelectedItem;//vacuumBox is a comboBox that contains the vacuums names 
      XmlNode rootElement = inventory.FirstChild.NextSibling;//first child is the xml encoding type tag not the root 

      int quantity, newQuantity = 0; 
      foreach (XmlNode device in rootElement.ChildNodes) 
      { 
       if (String.Equals(device["NAME"].InnerText, vacuumName)) 
       { 
        int number = Convert.ToInt32(vacuumsNumber.Value);//vacuumNumber is the name of the numeric up down 
        quantity = Int32.Parse(device["QUANTITY"].InnerText); 
        newQuantity = quantity + number; 
        device["QUANTITY"].InnerText = newQuantity.ToString();//Updating the QUANTITY node value. 
        inventory.Save("Inventory.xml"); 
        continue; 
       } 
      } 
0

嗯,我認爲在這種情況下迭代方法更容易理解。我們想要做的是找到一個XmlElement,它具有指定的Name元素和所需的內容。

它有助於將XmlDocument視爲一棵樹。這棵樹有不同的節點。最上面的節點是Inventory. The Inventory`-Node具有(在這種情況下)三個設備子節點。每個設備子節點也有子節點(名稱,品牌等)。考慮到這一點,很容易找到數量。我們遍歷樹並檢查名稱元素是否與搜索字符串匹配。如果是這樣,我們得到數量元素的值。這是一個小型控制檯應用程序,說明可能的解請記住,如果XML格式不正確(例如NAME元素丟失),此解決方案可能會導致異常。

static void Main(string[] args) 
    { 
     XmlDocument xdoc = new XmlDocument(); 

     string searchName = "Quietforce Bagged Canister"; 

     xdoc.LoadXml(@"<INVENTORY> 
         <DEVICE ID='1'> 
          <NAME>Air Steerable Bagless Upright</NAME> 
          <BRAND>Hoover</BRAND> 
          <MODEL>UH72400</MODEL> 
          <QUANTITY>23</QUANTITY> 
          <BUYING_PRICE>189.99</BUYING_PRICE> 
          <SELLING_PRICE>229.99</SELLING_PRICE> 
         </DEVICE> 
         <DEVICE ID='2'> 
          <NAME>Quietforce Bagged Canister</NAME> 
          <BRAND>Hoover</BRAND> 
          <MODEL>SH30050</MODEL> 
          <QUANTITY>18</QUANTITY> 
          <BUYING_PRICE>299.99</BUYING_PRICE> 
          <SELLING_PRICE>334.99</SELLING_PRICE> 
         </DEVICE> 
         <DEVICE ID='3'> 
          <NAME>Corded Cyclonic Stick Vacuum</NAME> 
          <BRAND>Hoover</BRAND> 
          <MODEL>SH20030</MODEL> 
          <QUANTITY>21</QUANTITY> 
          <BUYING_PRICE>79.99</BUYING_PRICE> 
          <SELLING_PRICE>109.99</SELLING_PRICE> 
         </DEVICE></INVENTORY>"); 

     //this is the INVENTORY element. You might need to customize that, in case the INVENTORY is not the root 
     XmlNode rootElement = xdoc.FirstChild; 

     //-1 means that no mathing item was found. 
     int quantity = -1; 

     //Here we iterate over every device element 
     foreach(XmlNode device in rootElement.ChildNodes) 
     { 
      //TODO: Validate XML first. Missing Elements can cause exceptions 

      //We can access the child elements of the decives with their element name 
      if(String.Equals(device["NAME"].InnerText, searchName)) 
      { 
       quantity = Int32.Parse(device["QUANTITY"].InnerText); 
       break; 
      } 
     } 

     Console.WriteLine(quantity.ToString()); 
     Console.ReadLine(); 
    } 
+0

您顯然也可以使用XPath執行此操作。看到http://stackoverflow.com/questions/9683054/xpath-to-select-element-based-on-childs-child-value – ChrisK

+0

我做了同樣的你,而不是xdoc.LoadXml(@「 ... 。「) 我做了這個xdoc.Load(」Inventory.xml「),但由於某種原因,該文件沒有加載,因爲編譯器甚至不在foreach循環內......任何想法爲什麼會發生這種情況?? – Xavier1819

+0

如果未進入foreach循環,則rootElement很可能沒有ChildNodes。您應該在分配rootElement之後設置一個斷點,以查看是否選擇了正確的rootElement。 – ChrisK

0

如果你更喜歡LINQ to SQL的方法,這樣的工作。

public string GetXML() 
{ 
    return @"<root><DEVICE ID=""1""> 
        <NAME>Air Steerable Bagless Upright</NAME> 
        <BRAND>Hoover</BRAND> 
        <MODEL>UH72400</MODEL> 
        <QUANTITY>23</QUANTITY> 
        <BUYING_PRICE>189.99</BUYING_PRICE> 
        <SELLING_PRICE>229.99</SELLING_PRICE> 
       </DEVICE> 
       <DEVICE ID=""2""> 
        <NAME>Quietforce Bagged Canister</NAME> 
        <BRAND>Hoover</BRAND> 
        <MODEL>SH30050</MODEL> 
        <QUANTITY>18</QUANTITY> 
        <BUYING_PRICE>299.99</BUYING_PRICE> 
        <SELLING_PRICE>334.99</SELLING_PRICE> 
       </DEVICE> 
       <DEVICE ID=""3""> 
        <NAME>Corded Cyclonic Stick Vacuum</NAME> 
        <BRAND>Hoover</BRAND> 
        <MODEL>SH20030</MODEL> 
        <QUANTITY>21</QUANTITY> 
        <BUYING_PRICE>79.99</BUYING_PRICE> 
        <SELLING_PRICE>109.99</SELLING_PRICE> 
       </DEVICE></root>"; 

} 

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    ///this would be your up down control 

    var xml = XDocument.Parse(GetXML()); 
    var device = xml.Descendants("DEVICE").Where(d => d.Descendants("NAME").First().Value == "Air Steerable Bagless Upright"); 
    var quantity = Convert.ToInt16(device.Descendants("QUANTITY").First().Value); 
    quantity++; 
    device.Descendants("QUANTITY").First().Value = quantity.ToString(); 
    xml.Save(@"c:\temp\temp.xml"); 
}