0

I'm Getting response from the remote server as a string which contains XML as below:

<?xml version="1.0" encoding="utf-16"?>
  <abcTransactionSegment>
    <TransactionSegment>
      <RECORD_TYPE>G100</RECORD_TYPE>
      <POS_TILL></POS_TILL>
      <SHIFT_NO></SHIFT_NO>
      <RECEIPT_NO>SC10333</RECEIPT_NO>
      <TIMESTAMP>2017-08-01T20:12:35.84+05:30</TIMESTAMP>
      <INV_AMT>1494.9</INV_AMT>
      <TAX_AMT>269.1</TAX_AMT>
      <DIS_AMT>1495</DIS_AMT>
      <NET_AMT>1764</NET_AMT>
      <RET_AMT>0</RET_AMT>
      <CUST_NAME>MAYUR</CUST_NAME>
      <TRANSACTION_STATUS>SALES</TRANSACTION_STATUS>
    </TransactionSegment>
    <TransactionSegment>
      .
      .
    </TransactionSegment>
 </abcTransactionSegment>

I want to read all child attributes of each TransactionSegment in different variables :

String RECEIPT_NO="SC10333";
String INV_AMT="1494.9"

Thanks in advance.

dev.Sumit
  • 49
  • 1
  • 9
  • 1
    Possible duplicate of [Iterating through all nodes in XML file](https://stackoverflow.com/questions/2915294/iterating-through-all-nodes-in-xml-file) – Joel Harkes Aug 30 '17 at 07:06
  • 2
    there are tons of answers for this question on this platform. please google your question first before. eg: https://stackoverflow.com/questions/2915294/iterating-through-all-nodes-in-xml-file – Joel Harkes Aug 30 '17 at 07:07

4 Answers4

1

Your can write your own XML parser or can use Linq2XML.

XElement xml = XElement.Parse(xmlResponseAsString);
IEnumerable<XElement> segmentItems =  xml.Elements("TransactionSegment");
Markus
  • 2,071
  • 4
  • 22
  • 44
Suleymani
  • 161
  • 6
0
    XmlDocument doc = new XmlDocument();
    //load xml
    doc.Load(yourXMLString);
    or
    doc.LoadXml("<?xml version="1.0" encoding="utf-16"?>
  <abcTransactionSegment>
    <TransactionSegment>
      <RECORD_TYPE>G100</RECORD_TYPE>
      <POS_TILL></POS_TILL>
      <SHIFT_NO></SHIFT_NO>
      <RECEIPT_NO>SC10333</RECEIPT_NO>
      <TIMESTAMP>2017-08-01T20:12:35.84+05:30</TIMESTAMP>
      <INV_AMT>1494.9</INV_AMT>
      <TAX_AMT>269.1</TAX_AMT>
      <DIS_AMT>1495</DIS_AMT>
      <NET_AMT>1764</NET_AMT>
      <RET_AMT>0</RET_AMT>
      <CUST_NAME>MAYUR</CUST_NAME>
      <TRANSACTION_STATUS>SALES</TRANSACTION_STATUS>
    </TransactionSegment>
    <TransactionSegment>
      .
      .
    </TransactionSegment>
 </abcTransactionSegment>");

    //finding node
    XmlNode node = doc.DocumentElement.SelectSingleNode("/abcTransactionSegment/TransactionSegment/RECORD_TYPE");
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
0

There are multiple ways to do it.Best way to do it using XML deserialize. Here is the alternative way to do using LINQ.

XDocument doc = XDocument.Load(xml);//Load your XML data here.
       if (doc.Descendants("abcTransactionSegment").Any()) // Check for root
       {

           if (doc.Descendants("abcTransactionSegment").Elements("TransactionSegment").Count() > 0)
           {
               foreach (var xmlElements in doc.Descendants("abcTransactionSegment").Elements("TransactionSegment"))
               {
                  string RECEIPT_NO =  xmlElements.Element("RECEIPT_NO") != null ? xmlElements.Element("RECEIPT_NO").Value : "";// It will give you the node value
               }
           }
       }
Oasis
  • 480
  • 3
  • 16
0

you can use XML Deserialize . Create a TransactionSegment Model

public int RECORD_TYPE {get;set;} 
pub string POS_TILL {get;set;} 

Dim a List<TransactionSegment> Model   listTransactionSegment

System.IO.StringReader stringReader = new System.IO.StringReader(Xml);  
System.Xml.Serialization.XmlSerializer xmlSerializer = new 
System.Xml.Serialization.XmlSerializer(typeof(List<TransactionSegment>));  


//  
listTransactionSegment=xmlSerializer.Deserialize(stringReader) as 
List<TransactionSegment>;  
amonk
  • 1,769
  • 2
  • 18
  • 27
jin hou
  • 1
  • 1