Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi all

XML
<AllowanceDeduction>
  <Deduction Name=" ROW 1" Amount="-99" />
  <allowance Name="ROW 2" Amount="88" />
</AllowanceDeduction>


I have a xml schema above like that



I want out put as follows

allowanceName allowanceamount  deductionName dectionAmount
ROW-2                 88                             ROW 1               -99


Please help me guys ..its very urgnet...
thanks
in advance


mail me xxxxxxxxxxxxxxx@gmail.com
Posted
Updated 6-Mar-15 9:06am
v3
Comments
PIEBALDconsult 6-Mar-15 15:07pm    
"..its very urgnet"

Maybe for you, but not for us. You should not include your email address in a public forum.
Have a look at XSLT.
http://www.w3schools.com/xsl/default.asp
Search how to parse XML to Table in c#.

1 solution

Hi,

I think you are not aware of XML parsing.As you have not specified whether you want parse xml to c# and move to DB or move the data directly to DB.I have created a basic example to help you with the xml for your understanding in C# as others have given you details for moving xml to sql.Basically what it does is it will loop through all the elements in your xml under the root
XML
<allowancededuction></allowancededuction>
. In each of the elements there are lot of attributes and each one we will loop through and get all the values and move it to a entity DataEntity. So now you can pass this DataEntity to you DataAccess Layer and insert into your DB.


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
             ParseXML();
             Console.Read();
        }

        public static void ParseXML()
        {
            string filePath=@"C:\File\Document1.xml";
            XDocument XMLDocument = XDocument.Load(filePath);

            List<dataentity> lstDataEntity = new List<dataentity>();

            foreach (XElement ele in XMLDocument.Root.Elements())
            {
                DataEntity de = new DataEntity();
                if (ele.Name == "allowance")
                {
                    foreach (XAttribute attr in ele.Attributes())
                    {
                        if (attr.Name == "Name")
                        {
                            de.AllowanceName = attr.Value;
                        }
                        else if (attr.Name == "Amount")
                        {
                            de.AllowanceAmount = attr.Value;
                        }
                    }
                }
                else if (ele.Name == "Deduction")
                {
                    foreach (XAttribute attr in ele.Attributes())
                    {
                        if (attr.Name == "Name")
                        {
                            de.DeductionName = attr.Value;
                        }
                        else if (attr.Name == "Amount")
                        {
                            de.DeductionName = attr.Value;
                        }
                    }
                }

                Console.WriteLine("AllowanceName :" + de.AllowanceName + " AllowanceAmount:" + de.AllowanceAmount + " DeductionName:  " + de.DeductionName + " DeductionAmount:" + de.DeductionAmount);
                lstDataEntity.Add(de);
             }
        }


    }

    public class DataEntity
    {
        private string allowanceName;

        public string AllowanceName
        {
            get { return allowanceName; }
            set { allowanceName = value; }
        }
        private string allowanceAmount;

        public string AllowanceAmount
        {
            get { return allowanceAmount; }
            set { allowanceAmount = value; }
        }
        private string deductionName;

        public string DeductionName
        {
            get { return deductionName; }
            set { deductionName = value; }
        }
        private string deductionAmount;

        public string DeductionAmount
        {
            get { return deductionAmount; }
            set { deductionAmount = value; }
        }

    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900