2017-11-02 5 views
-9

私のXMLのすべてのタグ名とタグ値を読み取るコードを作成しようとしていますが、それに近づくことはできません。XMLのC#コード

私のサンプルXMLは次のようである。この問題を解決してください

<TaxPayerDetails> 
    <TFN Tag="^AAD">1947</TFN> 
    <Title Tag="^ABE">MR</Title> 
    <GivenName Tag="^ABG">Mahesh</GivenName> 
    <MiddleName Tag="^BBB"></MiddleName> 
    <FamilyName Tag="^ABF">sharma</FamilyName> 
    <Suffix Tag="^BAW"/> 
    <ResidencyStatus Tag="^AJY">N</ResidencyStatus> 
    <ResidencyStatusDate Tag="">2017-10-31</ResidencyStatusDate> 
    <PostalAddress> 
     <Line1 Tag="^ABH">ABC company</Line1> 
     <Line2 Tag="^ABH">GPO</Line2> 
     <Suburb Tag="^AME">delhi</Suburb> 
     <State Tag="^AMF">delhi</State> 
     <PostCode Tag="^APE">2000</PostCode> 
    </PostalAddress> 
    <DateOfBirth Tag="^ABQ">1979-12-15</DateOfBirth> 
    <ContactPhone> 
     <AreaCode Tag="^BOC">900</AreaCode> 
     <PhoneNo Tag="^BOD">1234</PhoneNo> 
    </ContactPhone> 
</TaxPayerDetails> 
<PAYGPaymentSummaries> 
    <MostRecentPAYGUpdateDate>2017-06-30</MostRecentPAYGUpdateDate> 
    <IndividualNonBusinessPaymentSummary> 
     <IndividualNonBusinessPaymentSummaryType> 
      <LastUpdatedTimestamp Tag="">2017-06-30</LastUpdatedTimestamp 
     </IndividualNonBusinessPaymentSummaryType> 
    </IndividualNonBusinessPaymentSummary> 
</PAYGPaymentSummaries> 

+0

あなたのコードです? **私たちに**あなたがこれまでに試したことを見せてください!このXMLから何を抽出しようとしていますか?あなたはエラーを受け取りますか?もしそうなら、**何のエラーですか? –

+3

http://idownvotedbecau.se/noattempt/ – wp78de

+0

目次:無効なXMLです。そうでない場合は、[Xml2CSharp](http://xmltocsharp.azurewebsites.net/)を使用することもできます。 –

答えて

0

XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load("/*your file path here*/"); 
     XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("provide your nodes here"); 
     string val1="";val2="";val3=""; 
     foreach (XmlNode node in nodeList) 
     { 
      val1= node.SelectSingleNode("provide your xml nodes relevant tag here").InnerText; 
      val2= node.SelectSingleNode("provide your xml nodes relevant tag here").InnerText; 
      val3= node.SelectSingleNode("provide your xml nodes relevant tag here").InnerText; 
      MessageBox.Show(val1+ " " + val2+ " " + val3); 
     } 
0

は、私は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); 

      Dictionary<string, string> elements = doc.Descendants().Where(x => x.Attribute("Tag") != null) 
       .ToDictionary(x => x.Name.LocalName, y => (string)y); 
     } 
    } 
}