XML转SQL转换器c#

时间:2017-02-07 07:36:17

标签: c# sql xml-parsing

我编写了一个XML库但是我无法弄清楚为什么我不能 阅读我PC上的XML文件。我有大约1.5M的XML文件来解析并转换为SQL。 这是我的代码:

    class XMLConverter
        {
            public string XMLConverterSQL(int ID, string lang)
            {
                // int i = ID; // Starting Elements
                Stopwatch stopwatch = new Stopwatch();

                XmlDocument xml;
                XmlElement element;
                XmlNodeList nodes;
                string result;

                //Console.WriteLine(id_min);
                //Console.WriteLine(id_max);
                result = "";
                xml = new XmlDocument();
                xml.LoadXml("C:\\Users\\Phil1\\Desktop\\V1\\XMLDownloader\\bin\\Debug\\XMLData\\" + lang + "\\" + ID + ".xml");
                element = xml.DocumentElement;
                nodes = element.SelectNodes("/wowhead");
                if (xml.SelectSingleNode("/wowhead/error") != null)
                {
                    Console.WriteLine("Item ID {0} does not exist yet or not in WoWHead Database ", ID);
                }
                else
                {
                result += "INSERT INTO item_wh (entry, class, subclass, displayId, ,quality, name, level) VALUES (";
                foreach (XmlNode node in nodes)
                {
                    // entry
                    result += node["item"].Attributes["id"].InnerText;
                    result += ", ";
                    // class
                    result += node["item"]["class"].Attributes["id"].InnerText;
                    result += ", ";
                    // subclass
                    result += node["item"]["subclass"].Attributes["id"].InnerText;
                    result += ", ";
                    // displayId
                    result += node["item"]["icon"].Attributes["displayId"].InnerText;
                    result += ", ";
                    // quality
                    result += node["item"]["quality"].Attributes["id"].InnerText;
                    result += ", \"";
                    // name
                    result += node["item"]["name"].InnerText;
                    result += "\", ";
                    // level
                    result += node["item"]["level"].InnerText;
                    result += ");";
                    // bakcline
                    result += "\r\n";
                    }
                }
                return (result);
            }
        }

主要:

int min = int.Parse(txtMin_ID.Text);
int max = int.Parse(txtMax_ID.Text);
                List<string> results;

                results = new List<string>();
                // ParseXML.CreateDictionary("en");
                // ParseWH.startParse(min, max);  
                var sTab = new ConcurrentBag<int>(Enumerable.Range(min, max + 1));
                Parallel.ForEach(sTab, (i) =>
                {
                    Task.Factory.StartNew(() =>
                    {
                        string Convert = new XMLConverter().XMLConverterSQL(i, "EN");
                        lock (results)
                        {
                            results.Add(Convert);
                        }
                    });
                });
                Task.WaitAny();
                File.WriteAllText("C:\\Users\\Phil1\\Desktop\\result.sql", String.Join("", results));

and here is an example of XML if have to read

     <?xml version="1.0" encoding="UTF-8"?>
    -<wowhead>
    -<item id="105005">
    -<name>
    -<![CDATA[Windfire Legplates]]>
    </name>
    <level>528</level>
    <quality id="4">Epic</quality>
    -<class id="4">
    +<![CDATA[]]>
    </class>
    +<subclass id="4">
    -<![CDATA[Plate Armor]]>
    <icon displayId="127510">inv_plate_raidpaladin_n_01pant</icon>
    <inventorySlot id="7">Legs</inventorySlot>
    <upgrade id="491" level="0">0</upgrade>
    -<htmlTooltip>
    -<![CDATA[<table><tr><td><!--nstart--><b class="q4">Windfire Legplates</b><!--nend--><!--ndstart--><br /><span style="color: #00FF00">Raid Finder</span><!--ndend--><span style="color: #ffd100"><br />Item Level <!--ilvl-->528</span><br /><span style="color: #ffd100">Upgrade Level <!--uindex-->0/4</span><br /><!--bo-->Binds when picked up<table width="100%"><tr><td>Legs</td><th><!--scstart4:4--><span class="q1">Plate</span><!--scend--></th></tr></table><span><!--amr-->104 Armor</span><br /><span><!--stat5-->+57 Intellect</span><br /><span><!--stat7-->+97 Stamina</span><!--ebstats--><br /><span class="q2">+<!--rtg32-->33 Critical Strike&nbsp;<small>(<!--rtg%32-->0.08%&nbsp;@&nbsp;L<!--lvl-->110)</small></span><br /><span class="q2">+<!--rtg36-->43 Haste&nbsp;<small>(<!--rtg%36-->0.11%&nbsp;@&nbsp;L<!--lvl-->110)</small></span><!--egstats--><!--e--><br /><br /><a href="http://www.wowhead.com/items/gems?filter=81;9;0" class="socket-prismatic q0">Prismatic Socket</a><br /><a href="http://www.wowhead.com/items/gems?filter=81;9;0" class="socket-prismatic q0">Prismatic Socket</a><!--ps--><br /><!--sb--><span class="q0">Socket Bonus: +<!--ee16:0:90:750:0:0-->8 Intellect</span><br /><br />Durability 120 / 120</td></tr></table><table><tr><td>Requires Level <!--rlvl-->90<div class="whtt-sellprice">Sell Price: <span class="moneygold">34</span> <span class="moneysilver">79</span> <span class="moneycopper">3</span></div></td></tr></table><!--i?105005:1:110:90-->]]>
    </htmlTooltip>
    -<json>
    -<![CDATA["appearances":{"0":[127510,""]},"armor":104,"attainable":2,"classs":4,"displayid":127510,"flags2":8192,"id":105005,"level":528,"name":"4Windfire Legplates","namedesc":"Raid Finder","raidfinder":1,"reqlevel":90,"slot":7,"slotbak":7,"specs":[65],"subclass":4,"upgrades":[4,8,12,16]]]>
    </json>
    -<jsonEquip>
    -<![CDATA["appearances":{"0":[127510,""]},"armor":104,"critstrkrtng":33,"displayid":127510,"dura":120,"hastertng":43,"int":57,"nsockets":2,"reqlevel":90,"sellprice":347903,"slotbak":7,"socket1":7,"socket2":7,"socketbonus":4848,"sta":97]]>
    </jsonEquip>
    <link>http://www.wowhead.com/item=105005</link>
    </item>
    </wowhead>

为了获得更快的性能我试图将所有XML加载到ConcurrentDictionary中,但是我的内存运行速度非常快,所有XML文件各约为1到9 KB。

我收到了

  

根级别的数据无效。第1行第1位

但是,对于断点,

错误似乎正确地认为是XML转换器代码

1 个答案:

答案 0 :(得分:1)

没关系,删除任务工厂

解决了这个问题
var sTab = new ConcurrentBag<int>(Enumerable.Range(min, max));
Parallel.ForEach(sTab, (i) =>
            {
                string Convert = new XMLConverter().XMLConverterSQL(i, "EN");
                lock (results)
                {
                    results.Add(Convert);
                }
            });