Sunday, April 21, 2013

XML manipulation in C# - reading from two nodes, concatenating them and adding the value as a new node.

Hello,

I am a C# beginner and I had the requirement of reading two nodes from an XML file concatenating them and inserting the concatenated value in a new node.

The code I used is given below, and hopefully will help you to get your task done (which should not be an evil purpose :) ). The sample XML used can be found below the code.

What is being done:
- Read the values from a node and add them to a list (using XmlNodeList and List<string>).
- Do the same for the other node from which I need the values.
- Concat the values of the two lists above.
- Add the concatenated values as a new node to the XML (using XmlDocument 's CreateNode method).

Code:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
//use XmlDocument to access the XML
            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("C:\\Documents and Settings\\learner\\Desktop\\Daily\\XML\\A.xml");
            xmlDoc.LoadXml(response);

//Get the node list into a XmlNodeList object using XmlDocument 's SelectNodes method
            XmlNodeList CustomerNumList = xmlDoc.SelectNodes("//CustomerChosen/Customer/CustomerID/CustomerNo/text()");

            List<string> numberVals = new List<string>();

//Iterate the XmlNodeList
            for (int i = 0; i < CustomerNumList.Count; i++)
            {
                numberVals.Add(CustomerNumList[i].Value);
            }

            List<string> functionCodeVals = new List<string>();

//same as above - but a different node
            XmlNodeList CustomerFunctionList = xmlDoc.SelectNodes("//CustomerChosen/Customer/CustomerFunction/Code/text()");
            for (int i = 0; i < CustomerFunctionList.Count; i++)
            {
                functionCodeVals.Add(CustomerFunctionList[i].Value);
            }

            List<string> concatNumCodes = new List<string>();

//Concatenating here
            for (int i = 0; i < numberVals.Count; i++)
                concatNumCodes.Add(numberVals[i] + "-" + functionCodeVals[i]);

            string concatNumCodeString = String.Join(",", concatNumCodes);
           

            XmlNodeList CustomerSelectionList = xmlDoc.SelectNodes("//CustomerSelection/Customer");
            for (int i = 0; i < CustomerSelectionList.Count; i++)
            {
//making a new node here using XmlDocument's CreateNode method
                XmlNode nodeDeterminedCustomers = xmlDoc.CreateNode("element", "DeterminedCustomers", "");
                nodeDeterminedCustomers.InnerText = concatNumCodeString;
                CustomerSelectionList[i].AppendChild(nodeDeterminedCustomers);

            }

            Console.WriteLine(concatNumCodeString);
            xmlDoc.Save(Console.Out);
            Console.ReadLine();
// sharing to make your life easy, hope this helps ! good luck :)

        }

    }

}



XML:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP:Body>
      <ns0:CustomerDetermByElemResp xmlns:ns0="http://learner.com/CRM/BusinessCustomer">
        <CustomerChosen>
            <Customer>
               <CustomerID>
                  <CustomerNo>6321565426</CustomerNo>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000003</Code>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <DeterminationTypeCode>2</DeterminationTypeCode>
            </Customer>
            <Customer>
               <CustomerID>
                  <CustomerNo>5236214589</CustomerNo>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000004</Code>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <DeterminationTypeCode>2</DeterminationTypeCode>
            </Customer>
         </CustomerChosen>
         <CustomerSelection>
            <Customer>
               <CustomerID>
                  <CustomerNo>953216475</CustomerNo>
                  <CustomerGUID>DE435FD45T</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000015</Code>
                  <Text>Contact Person</Text>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <CustomerDescription>0430 Peter</CustomerDescription>
               <CustomerAddress>1265 SriLanka</CustomerAddress>
               <DeterminationTypeCode>1</DeterminationTypeCode>
            </Customer>
            <Customer>
               <CustomerID>
                  <CustomerNo>2153654892</CustomerNo>
                  <CustomerGUID>E234R32ED4</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000123</Code>
                  <Text>Contact Person</Text>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <CustomerDescription>0124 John</CustomerDescription>
               <CustomerAddress>1234 Srilanka</CustomerAddress>
               <DeterminationTypeCode>1</DeterminationTypeCode>
            </Customer>
         </CustomerSelection>
         <CustomerList/>
      </ns0:CustomerDetermByElemResp>
   </SOAP:Body>
</SOAP:Envelope>

No comments:

Post a Comment