2016-03-25 36 views
-3

我需要爲ID返回的值=「3」和ID =「4」到標籤C#返回XML從字符串屬性值標籤

XML字符串被存儲在數據庫列完全一樣:

<Attributes><CheckoutAttribute ID="3"><CheckoutAttributeValue><Value>dear jason, wishing you a happy easter</Value></CheckoutAttributeValue></CheckoutAttribute><CheckoutAttribute ID="4"><CheckoutAttributeValue><Value>Thursday, 31-03-2016</Value></CheckoutAttributeValue></CheckoutAttribute></Attributes> 

我期待得到如下輸出。

Label1.Text =「親愛的傑森,祝你復活節快樂」;

Label2.Text =「Thursday,31-03-2016」;

Label1的永遠是ID = 「3」 Label2的永遠是ID = 「4」

感謝

+1

歡迎計算器。這是一個網站,詢問「我有什麼問題」......它不是**,而是說「請爲我做代碼」。請找一個教程(互聯網上有負載),給它一個去...當你掙扎時,告訴我們你有什麼(在[最小,完整和可驗證的例子])(http:// stackoverflow .com/help/mcve)),我們會盡力幫忙。請閱讀幫助中的[問]部分,以及[優秀文章](http://whathaveyoutried.com)。 – freefaller

+0

http://stackoverflow.com/questions/3750678/getting-attribute-value-of-an-xml-document-using-c-sharp –

回答

0

嘗試這樣的事情來提取你的字符串....

Usings ...

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Serialization; 

類...(使用XML在http://xmltocsharp.azurewebsites.net/創建)

[XmlRoot(ElementName = "CheckoutAttributeValue")] 
    public class CheckoutAttributeValue 
    { 
     [XmlElement(ElementName = "Value")] 
     public string Value { get; set; } 
    } 

    [XmlRoot(ElementName = "CheckoutAttribute")] 
    public class CheckoutAttribute 
    { 
     [XmlElement(ElementName = "CheckoutAttributeValue")] 
     public CheckoutAttributeValue CheckoutAttributeValue { get; set; } 
     [XmlAttribute(AttributeName = "ID")] 
     public string ID { get; set; } 
    } 

    [XmlRoot(ElementName = "Attributes")] 
    public class Attributes 
    { 
     [XmlElement(ElementName = "CheckoutAttribute")] 
     public List<CheckoutAttribute> CheckoutAttribute { get; set; } 
    } 

代碼....

 string strXML = @"<Attributes> 
           <CheckoutAttribute ID=""3""> 
            <CheckoutAttributeValue> 
             <Value>dear jason, wishing you a happy easter</Value> 
            </CheckoutAttributeValue> 
           </CheckoutAttribute> 
           <CheckoutAttribute ID=""4""> 
            <CheckoutAttributeValue> 
             <Value>Thursday, 31-03-2016</Value> 
            </CheckoutAttributeValue> 
           </CheckoutAttribute> 
          </Attributes>"; 

     byte[] bufAttributes = ASCIIEncoding.UTF8.GetBytes(strXML); 
     MemoryStream ms1 = new MemoryStream(bufAttributes); 

     // Deserialize to object 
     XmlSerializer serializerPlaces = new XmlSerializer(typeof(Attributes)); 
     try 
     { 
      using (XmlReader reader = new XmlTextReader(ms1)) 
      { 
       Attributes deserializedXML = (Attributes)serializerPlaces.Deserialize(reader); 

       string Label1Text = (from xmlTag in deserializedXML.CheckoutAttribute where xmlTag.ID == "3" select xmlTag.CheckoutAttributeValue.Value).FirstOrDefault(); 
       string Label2Text = (from xmlTag in deserializedXML.CheckoutAttribute where xmlTag.ID == "4" select xmlTag.CheckoutAttributeValue.Value).FirstOrDefault(); 

      }// put a break point here and mouse-over Label1Text and Label2Text …. 
     } 
     catch (Exception ex) 
     { 
      throw; 
     } 
+0

非常感謝Monty,第一次工作完美 – jasonkkt