C# 基于数据算法的XML结构化与多XML

C# 基于数据算法的XML结构化与多XML,c#,xml,algorithm,C#,Xml,Algorithm,这是我的桌子: 项目稳定 Id、代码、价格、重量 我想使用C#根据这些规则生成一个或多个xml文件 xml中的项不能超过99项,如果超过99项,则应启动一个新的xml文件。MyItems中的项目不能超过99个,如果有更多,则应启动新的MyCollection <xml> <MyCollections> <!--- 0 to 99 MyCollection items allowed--> <MyCollection> <Cod

这是我的桌子:

项目稳定 Id、代码、价格、重量

我想使用C#根据这些规则生成一个或多个xml文件

xml中的项不能超过99项,如果超过99项,则应启动一个新的xml文件。MyItems中的项目不能超过99个,如果有更多,则应启动新的MyCollection

<xml>
 <MyCollections>
 <!--- 0 to 99 MyCollection items allowed-->
  <MyCollection>
   <Code>6201110000</Code>
   <MyItems>
     <!--- 0 to 99 MyItems allowed -->
     <MyItem>
        <Sequence>1</Price>
        <Price>10</Price>
        <Weight>20</Weight>
     </MyItem>

     ....

     <MyItem>
        <Sequence>99</Price>
        <Price>300</Price>
        <Weight>2</Weight>
     </MyItem>
  </Item>
</MyCollection>

<MyCollection>
 <Code>6201110000</Code>
  <MyItems>     
    <MyItem>
        <Sequence>100</Price>
        <Price>10</Price>
        <Weight>20</Weight>
    </MyItem>
    .....

</MyCollection>

您可以使用take和skip查询表

假设您有两种方法:

    private IList<string> Query(string s)
    {
        // Execute your SQL query 
    }

    private void SerializeItems(IList<object> itemsList)
    {
        //Serialize your itemslist to an xml file
    }

循环是复杂的。我想我是对的。我使用Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string PATH = @"c:\temp\";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("Codes", typeof(string));
            dt.Columns.Add("Price", typeof(decimal));
            dt.Columns.Add("Weight", typeof(int));

            dt.Rows.Add(new object[] { "1", "620111000", 1.00, 20 });
            dt.Rows.Add(new object[] { "2", "610433000", 4.00, 30 });
            dt.Rows.Add(new object[] { "3", "620111005", 7.00, 50 });
            dt.Rows.Add(new object[] { "4", "620111006", 6.00, 60 });
            dt.Rows.Add(new object[] { "5", "620111006", 5.00, 70 });

            string xml = "<xml><MyCollections></MyCollections></xml>";

            var groups = dt.AsEnumerable().OrderBy(x => x.Field<string>("Codes")).GroupBy(x => x.Field<string>("Codes")).ToList();


            XDocument doc = XDocument.Parse(xml);
            XElement myCollections = doc.Descendants("MyCollections").FirstOrDefault();

            int sequence = 0;
            int file_count = 1;
            XElement myCollection = null;
            XElement myItems = null;
            string firstGroupName = "";
            string code = "";

            foreach (var group in groups)
            {
                code = group.Key;
                //only add here if there is not 99 items in file
                //other wise it will be added later
                if (sequence < 99)
                {
                    myCollection = new XElement("MyCollection", new XElement("Code", code));
                    myCollections.Add(myCollection);
                    myItems = new XElement("MyItems");
                    myCollection.Add(myItems);

                    firstGroupName = group.Key;
                }

                foreach (DataRow row in group)
                {
                    sequence++;
                    if (sequence > 99)
                    {
                        if (firstGroupName == code)
                        {
                            doc.Save(PATH + code + "_" + file_count.ToString() + ".xml");
                        }
                        else
                        {
                            doc.Save(PATH + firstGroupName + "_" + code + "_" + file_count.ToString() + ".xml");
                        }
                        file_count++;

                        sequence = 1;
                        doc = XDocument.Parse(xml);
                        myCollections = doc.Descendants("MyCollections").FirstOrDefault();

                        myCollection = new XElement("MyCollection", new XElement("Code", code));
                        myCollections.Add(myCollection);
                        myItems = new XElement("MyItems");
                        myCollection.Add(myItems);
                    }

                    XElement myItem = new XElement("MyItem", new object[] {
                        new XElement("Sequence", sequence),
                        new XElement("Price", row.Field<decimal>("Price")),
                        new XElement("Weight", row.Field<int>("Weight"))
                    });
                    myItems.Add(myItem);
                }
            }
            if (myItems.Elements("MyItem") != null)
            {
                if (firstGroupName == code)
                {
                    doc.Save(PATH + code + "_" + file_count.ToString() + ".xml");
                }
                else
                {
                    doc.Save(PATH + firstGroupName + "_" + code + "_" + file_count.ToString() + ".xml");
                }
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串路径=@“c:\temp\”;
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“Id”,类型(字符串));
添加(“代码”,类型(字符串));
添加(“价格”,类型(十进制));
添加(“重量”,类型(int));
添加(新对象[]{“1”,“62011000”,1.00,20});
添加(新对象[]{“2”,“610433000”,4.00,30});
添加(新对象[]{“3”,“62011005”,7.00,50});
添加(新对象[]{“4”,“62011006”,6.00,60});
添加(新对象[]{“5”,“62011006”,5.00,70});
字符串xml=”“;
var groups=dt.AsEnumerable().OrderBy(x=>x.Field(“代码”)).GroupBy(x=>x.Field(“代码”).ToList();
XDocument doc=XDocument.Parse(xml);
XElement myCollections=doc.substands(“myCollections”).FirstOrDefault();
int序列=0;
int file_count=1;
XElement myCollection=null;
XElement myItems=null;
字符串firstGroupName=“”;
字符串代码=”;
foreach(组中的var组)
{
代码=group.Key;
//仅当文件中没有99项时才在此处添加
//另一方面,它将在以后添加
if(序列<99)
{
myCollection=新的XElement(“myCollection”,新的XElement(“Code”,Code));
myCollections.Add(myCollection);
myItems=新XElement(“myItems”);
myCollection.Add(myItems);
firstGroupName=group.Key;
}
foreach(组中的数据行)
{
序列++;
如果(序列>99)
{
if(firstGroupName==代码)
{
doc.Save(路径+代码+“u”+文件\u count.ToString()+“.xml”);
}
其他的
{
doc.Save(PATH+firstGroupName+“”+code+“”+file\u count.ToString()+“.xml”);
}
文件计数++;
序列=1;
doc=XDocument.Parse(xml);
myCollections=doc.substands(“myCollections”).FirstOrDefault();
myCollection=新的XElement(“myCollection”,新的XElement(“Code”,Code));
myCollections.Add(myCollection);
myItems=新XElement(“myItems”);
myCollection.Add(myItems);
}
XElement myItem=新XElement(“myItem”,新对象[]{
新的元素(“序列”,序列),
新XElement(“价格”,行字段(“价格”),
新元素(“权重”,行字段(“权重”))
});
添加(myItem);
}
}
if(myItems.Elements(“MyItem”)!=null)
{
if(firstGroupName==代码)
{
doc.Save(路径+代码+“u”+文件\u count.ToString()+“.xml”);
}
其他的
{
doc.Save(PATH+firstGroupName+“”+code+“”+file\u count.ToString()+“.xml”);
}
}
}
}
}

到目前为止,您自己尝试过什么吗?您已经概述了需求,但没有描述在尝试实现此功能时遇到的实际问题。“我想使用C#根据这些规则生成一个或多个xml文件。”您的问题是我们是否愿意?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string PATH = @"c:\temp\";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("Codes", typeof(string));
            dt.Columns.Add("Price", typeof(decimal));
            dt.Columns.Add("Weight", typeof(int));

            dt.Rows.Add(new object[] { "1", "620111000", 1.00, 20 });
            dt.Rows.Add(new object[] { "2", "610433000", 4.00, 30 });
            dt.Rows.Add(new object[] { "3", "620111005", 7.00, 50 });
            dt.Rows.Add(new object[] { "4", "620111006", 6.00, 60 });
            dt.Rows.Add(new object[] { "5", "620111006", 5.00, 70 });

            string xml = "<xml><MyCollections></MyCollections></xml>";

            var groups = dt.AsEnumerable().OrderBy(x => x.Field<string>("Codes")).GroupBy(x => x.Field<string>("Codes")).ToList();


            XDocument doc = XDocument.Parse(xml);
            XElement myCollections = doc.Descendants("MyCollections").FirstOrDefault();

            int sequence = 0;
            int file_count = 1;
            XElement myCollection = null;
            XElement myItems = null;
            string firstGroupName = "";
            string code = "";

            foreach (var group in groups)
            {
                code = group.Key;
                //only add here if there is not 99 items in file
                //other wise it will be added later
                if (sequence < 99)
                {
                    myCollection = new XElement("MyCollection", new XElement("Code", code));
                    myCollections.Add(myCollection);
                    myItems = new XElement("MyItems");
                    myCollection.Add(myItems);

                    firstGroupName = group.Key;
                }

                foreach (DataRow row in group)
                {
                    sequence++;
                    if (sequence > 99)
                    {
                        if (firstGroupName == code)
                        {
                            doc.Save(PATH + code + "_" + file_count.ToString() + ".xml");
                        }
                        else
                        {
                            doc.Save(PATH + firstGroupName + "_" + code + "_" + file_count.ToString() + ".xml");
                        }
                        file_count++;

                        sequence = 1;
                        doc = XDocument.Parse(xml);
                        myCollections = doc.Descendants("MyCollections").FirstOrDefault();

                        myCollection = new XElement("MyCollection", new XElement("Code", code));
                        myCollections.Add(myCollection);
                        myItems = new XElement("MyItems");
                        myCollection.Add(myItems);
                    }

                    XElement myItem = new XElement("MyItem", new object[] {
                        new XElement("Sequence", sequence),
                        new XElement("Price", row.Field<decimal>("Price")),
                        new XElement("Weight", row.Field<int>("Weight"))
                    });
                    myItems.Add(myItem);
                }
            }
            if (myItems.Elements("MyItem") != null)
            {
                if (firstGroupName == code)
                {
                    doc.Save(PATH + code + "_" + file_count.ToString() + ".xml");
                }
                else
                {
                    doc.Save(PATH + firstGroupName + "_" + code + "_" + file_count.ToString() + ".xml");
                }
            }

        }
    }
}