C#将XML属性值从String返回到Label

时间:2016-03-25 10:48:01

标签: c# asp.net xml

我需要返回ID =" 3"的值。和ID =" 4"到标签

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 =&#34;亲爱的杰森,祝你复活节快乐&#34 ;;

Label2.Text =&#34; 2016年3月31日星期四&#34 ;;

Label1将始终用于ID =&#34; 3&#34; Label2将始终用于ID =&#34; 4&#34;

由于

1 个答案:

答案 0 :(得分:0)

尝试这样的东西来提取你的字符串......

... Usings

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

Classes ...(使用您在http://xmltocsharp.azurewebsites.net/处的XML创建)

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