如何根据name属性读取xml并获取值

时间:2015-05-19 13:03:27

标签: c# xml

<root>
    <Message type="sms">
        <Details locale="en" message-type="User.ResetPassword" />
        <Context>
            <Parameter name="Time" value=" 16:03:31" />
            <Parameter name="pswr" value="00" />
            <Parameter name="Date" value="18/12/2014" />
        </Context>
        <Receiver>+923328749199</Receiver>
    </Message>
</root>

我该如何阅读此文件?例如,我想获得时间,密码和日期的值,以及接收者标记值。

3 个答案:

答案 0 :(得分:0)

你的班级将是这样的。

   /*------------------------------------------------------------------------------
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------*/

namespace AutoGeneratedCode
{

   /// 
   [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
   [System.SerializableAttribute()]
   [System.Diagnostics.DebuggerStepThroughAttribute()]
   [System.ComponentModel.DesignerCategoryAttribute("code")]
   [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
   [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
   public partial class root
   {
      private rootMessage messageField;
      /// 
      public rootMessage Message
      {
         get
         {
            return this.messageField;
         }
         set
         {
            this.messageField = value;
         }
      }
   }
   /// 
   [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
   [System.SerializableAttribute()]
   [System.Diagnostics.DebuggerStepThroughAttribute()]
   [System.ComponentModel.DesignerCategoryAttribute("code")]
   [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
   public partial class rootMessage
   {
      private rootMessageDetails detailsField;
      private rootMessageParameter[] contextField;
      private decimal receiverField;
      private string typeField;
      /// 
      public rootMessageDetails Details
      {
         get
         {
            return this.detailsField;
         }
         set
         {
            this.detailsField = value;
         }
      }
      /// 
      [System.Xml.Serialization.XmlArrayItemAttribute("Parameter", IsNullable=false)]
      public rootMessageParameter[] Context
      {
         get
         {
            return this.contextField;
         }
         set
         {
            this.contextField = value;
         }
      }
      /// 
      public decimal Receiver
      {
         get
         {
            return this.receiverField;
         }
         set
         {
            this.receiverField = value;
         }
      }
      /// 
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string type
      {
         get
         {
            return this.typeField;
         }
         set
         {
            this.typeField = value;
         }
      }
   }
   /// 
   [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
   [System.SerializableAttribute()]
   [System.Diagnostics.DebuggerStepThroughAttribute()]
   [System.ComponentModel.DesignerCategoryAttribute("code")]
   [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
   public partial class rootMessageDetails
   {
      private string localeField;
      private string messagetypeField;
      /// 
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string locale
      {
         get
         {
            return this.localeField;
         }
         set
         {
            this.localeField = value;
         }
      }
      /// 
      [System.Xml.Serialization.XmlAttributeAttribute("message-type")]
      public string messagetype
      {
         get
         {
            return this.messagetypeField;
         }
         set
         {
            this.messagetypeField = value;
         }
      }
   }
   /// 
   [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
   [System.SerializableAttribute()]
   [System.Diagnostics.DebuggerStepThroughAttribute()]
   [System.ComponentModel.DesignerCategoryAttribute("code")]
   [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
   public partial class rootMessageParameter
   {
      private string nameField;
      private System.DateTime valueField;
      private bool valueFieldSpecified;
      /// 
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string name
      {
         get
         {
            return this.nameField;
         }
         set
         {
            this.nameField = value;
         }
      }
      /// 
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public System.DateTime value
      {
         get
         {
            return this.valueField;
         }
         set
         {
            this.valueField = value;
         }
      }
      /// 
      [System.Xml.Serialization.XmlIgnoreAttribute()]
      public bool valueSpecified
      {
         get
         {
            return this.valueFieldSpecified;
         }
         set
         {
            this.valueFieldSpecified = value;
         }
      }
   }
}
代码反序列化后

var serializer = new XmlSerializer(typeof(root));

using (var file = File.OpenText("sample.xml"))
{
    root data = (root)serializer.Deserialize(file);

    // ... 
}

您可以使用数据按root对象访问所有成员。

答案 1 :(得分:0)

希望帮助..........

Dim Xdoc As XDocument
    Xdoc = XDocument.Parse(pstrXmlInput)
    Dim Tktno = From Attbvalue In Xdoc.Descendants("Context").Elements("Parameter").Attributes("value")
                Select Attbvalue.Value

enter image description here

enter image description here

答案 2 :(得分:0)

使用XDocument

这是我的解决方案:

如果您的xml来自网址,请使用XDocument.Load(/ 网址 /)

var xml = "<root><Message type='sms'><Details locale='en' message-type='User.ResetPassword' /><Context><Parameter name='Time' value=' 16:03:31' /><Parameter name='pswr' value='00' /><Parameter name='Date' value='18/12/2014' /></Context><Receiver>+923328749199</Receiver></Message></root>";
var document = XDocument.Parse(xml);

var messages = document.Root.Elements();
foreach (var message in messages)
{
    var context = message.Element("Context");
    var parameters = context.Elements("Parameter");
    foreach (var parameter in parameters)
    {
        var name = parameter.Attribute("name").Value;
        var value = parameter.Attribute("value").Value;

        Console.WriteLine ("Parameter {0} with value of {1}", name, value);
    }

    Console.WriteLine ("Receiver {0}", message.Element("Receiver").Value);
}

<强>输出

Parameter Time with value of  16:03:31
Parameter pswr with value of 00
Parameter Date with value of 18/12/2014
Receiver +923328749199