从xml中检索值 [英] Retrieve values from xml

查看:62
本文介绍了从xml中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



hi all

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





我有一个类似的xml架构







我想要输出如下





I have a xml schema above like that



I want out put as follows

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





请帮帮我们..非常喜欢...

谢谢

提前





邮寄给我xxxxxxxxxxxxxxx@gmail.com



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


mail me xxxxxxxxxxxxxxx@gmail.com

推荐答案





我认为你不知道XML解析。你还没有说明是否你想要解析xml到c#并移动到DB或直接将数据移动到DB。我已经创建了一个基本的例子来帮助你使用xml来理解你在C#中,因为其他人已经为你提供了将xml移动到sql的详细信息。基本上它是什么它它会循环遍历xml下的所有元素
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
<allowancededuction></allowancededuction>

。在每个元素中都有很多属性,每个属性我们将遍历并获取所有值并将其移动到实体DataEntity。所以现在你可以将这个DataEntity传递给你的DataAccess Layer并插入你的数据库。





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


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; }
        }

    }
}


这篇关于从xml中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆