2016-08-15 304 views
0

我需要顯示processingStatus和每個字段節點值及其相應的以下XML名稱。XML屬性c#

<?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> 

任何人都可以幫助我嗎? 我的代碼: 編輯的代碼:

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

      } 
      Console.ReadLine(); 
+0

你有什麼企圖?任何搜索努力? – Steve

+0

@Steve yes請參閱編輯 – velvt

+0

現在請描述代碼有什麼問題以及輸出/異常 – Steve

回答

1

一種可能性是定義一個類型或多種類型反映了XML的結構;成員必須被賦予屬於XmlElementAttribute的屬性here和屬性XmlAttributeAttribute,其被記錄爲here;如果合適,文件內容可以使用XmlDeserializer類進行反序列化,該類記錄爲here。有關如何使用反序列化設施的詳細說明,請參見this官方教程。

1

嘗試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(); 
     } 
    } 
} 
+0

@jdwend請參閱編輯代碼 – velvt

+0

xml文件有多大?小XML文件不需要XmlReader。什麼標籤重複?我可以修改代碼來使用xml閱讀器,但需要重複的元素。如果你的輸入是一個字符串,那麼簡單地用Parse(你的xml字符串)替換Load(FILENAME)。 – jdweng

+0

如何輸出所有結果?即processingStatus和fields? – velvt