Beispiel #1
0
        private static void CreateNewXmls(string xmlName, Excel._Worksheet sheet)
        {
            XDocument xDoc = new XDocument();

            XElement root = new XElement("Root");

            xDoc.Add(root);

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

            int clomn = 1;

            while (true)
            {
                if (sheet.ReadExcelCell(2, 1) != "id" || sheet.ReadExcelCell(3, 1) != "Int32")
                {
                    Console.WriteLine("第一个属性应该为id!");
                    return;
                }

                string name = sheet.ReadExcelCell(2, clomn);
                string type = sheet.ReadExcelCell(3, clomn);
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(type))
                {
                    break;
                }

                itemNameList.Add(name + "_" + type);
                clomn++;
            }

            int  row     = 4;
            bool hasNext = true;

            while (hasNext)
            {
                if (string.IsNullOrEmpty(sheet.ReadExcelCell(row, 1)))
                {
                    hasNext = false;
                    break;
                }
                else
                {
                    XElement item = new XElement("item");
                    for (int i = 1; i < itemNameList.Count; i++)
                    {
                        string   itemName = itemNameList[i];
                        string   value    = sheet.ReadExcelCell(row, i + 1);
                        XElement el       = new XElement(itemName);
                        el.Value = value;
                        item.Add(el);
                    }
                    row++;
                    root.Add(item);
                }
            }
            string outputFile = Path.Combine(localGameDataXmlPath, xmlName);

            Console.WriteLine("生成Xml" + outputFile);
            xDoc.Save(outputFile);
        }