简体   繁体   English

XML属性C#

[英]XML attributes c#

I need to display the processingStatus and every field node value along with it's corresponding name of the following XML. 我需要显示processingStatus和每个字段节点值以及以下XML的对应名称。

<?xml version="1.0" encoding="UTF-8"?>
<T24 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.temenos.com/T24/OFSML/130" xsi:schemaLocation="http://www.temenos.com/T24/OFSML/130 ofsml13.xsd">
  <serviceResponse>
    <ofsTransactionProcessed application="FUNDS.TRANSFER" function="INPUT" operation="VALIDATE" processingStatus="OK" version="BOOK.TRAN.VAL.MCB">
      <transactionId>FT15056T2QLP</transactionId>
      <field mv="1" name="TRANSACTION.TYPE" sv="1">AC</field>
      <field mv="1" name="CURRENCY.MKT.DR" sv="1">1</field>
      <field mv="1" name="DEBIT.CURRENCY" sv="1">USD</field>
      <field mv="1" name="DEBIT.AMOUNT" sv="1">125.00</field>
      <field mv="1" name="DEBIT.VALUE.DATE" sv="1">20150225</field>

    </ofsTransactionProcessed>
  </serviceResponse>
</T24>

Can anyone help me please? 谁能帮我吗? my code: EDITED CODE: 我的代码:编辑代码:

XmlTextReader reader = new XmlTextReader("XML03.xml");
            Console.WriteLine(reader.Attributes["processingStatus"].Value);
            while (reader.Read())
            {
                Console.WriteLine(reader.Value);

            }
            Console.ReadLine();

One possibility would be to define a type or several types which reflect the structure of the Xml; 一种可能性是定义一个或多个反映Xml结构的类型。 the members would have to be endowed with the attributes XmlElementAttribute which is documented here , and the attribute XmlAttributeAttribute , which is documented here ; 成员必须与属性被赋予XmlElementAttribute其被记录在这里 ,属性XmlAttributeAttribute ,其被记录在这里 ; if done suitably, the file contents can be deserialized with the XmlDeserializer class, which is documented here . 如果可以的话,可以使用XmlDeserializer类对文件内容进行反序列化,该文档在此处进行了介绍 A detailed explanation on how to use the deserialization facilities can be found in this official tutorial. 可以在官方教程中找到有关如何使用反序列化功能的详细说明。

Try xml linq 试试xml linq

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var results = doc.Descendants().Where(x => x.Name.LocalName == "ofsTransactionProcessed").Select(x => new {
                application = (string)x.Attribute("application"),
                function = (string)x.Attribute("function"),
                operation = (string)x.Attribute("operation"),
                processingStatus = (string)x.Attribute("processingStatus"),
                version = (string)x.Attribute("version"),
                transactionId = x.Elements().Where(y => y.Name.LocalName == "transactionId").Select(z => (string)z).FirstOrDefault(),
                fields = x.Elements().Where(y => y.Name.LocalName == "field").Select(z => new {
                    mv = (string)z.Attribute("mv"),
                    name = (string)z.Attribute("name"),
                    sv = (string)z.Attribute("sv"),
                    value = (string)z
                }).ToList()
            }).FirstOrDefault();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM