2012-05-17 63 views
0

我是一個帶有asp.net的nube。這是我想要實現的。我想將以下XML數據寫入sql server數據庫的單個表中。讀取多級XML並寫回單個數據庫表

- <RaceDay RaceDayDate="2012-05-16T00:00:00" Year="2012" Month="5" Day="16" DayOfTheWeek="Wednesday" MonthLong="May" IsCurrentDay="1" IsPresaleMeeting="0" ServerTime="2012-05-16T16:47:30.033"> 
- <Race RaceNo="7" RaceTime="2012-05-16T16:36:00" CloseTime="2012-05-16T16:40:08.107" RaceName="F&M BM 70 HANDICAP" Distance="1200" SubFav="1" RaceDisplayStatus="PAYING" WeatherChanged="N" WeatherCond="1" WeatherDesc="Fine" TrackChanged="N" TrackCond="1" TrackDesc="Good" TrackRating="3" TrackRatingChanged="N"> 
- <TipsterTip TipsterId="0" Tips="1"> 
    <Tipster TipsterName="LATE MAIL" /> 
    </TipsterTip> 
- <TipsterTip TipsterId="1" Tips="8-5-3-7"> 
    <Tipster TipsterName="RADIO TAB" /> 
    </TipsterTip> 
- <TipsterTip TipsterId="2" Tips="1-10-9-2"> 
    <Tipster TipsterName="KEVIN CASEY" /> 
    </TipsterTip> 
- <Pool PoolType="EX" Available="Y" Abandoned="N" PoolDisplayStatus="PAYING" PoolTotal="3734.00" JPotInGross="0.00" JPotOutGross="0.00" LastCalcTime="2012-05-16T16:36:57" CalcTime="2012-05-16T16:41:31" StatDiv="0"> 
- <Dividend DivId="62406790" DivAmount="80.8000"> 
    <DivResult LegNo="1" RunnerNo="7" /> 
    <DivResult LegNo="2" RunnerNo="5" /> 
    </Dividend> 
    </Pool> 
- <Pool PoolType="F4" Available="Y" Abandoned="N" PoolDisplayStatus="PAYING" PoolTotal="3492.00" JPotInGross="0.00" JPotOutGross="1397.16" LastCalcTime="2012-05-16T16:36:57" CalcTime="2012-05-16T16:41:31" StatDiv="0"> 
- <Dividend DivId="62406797" DivAmount="10828.0000"> 
    <DivResult LegNo="1" RunnerNo="7" /> 
    <DivResult LegNo="2" RunnerNo="5" /> 
    <DivResult LegNo="3" RunnerNo="13" /> 
    <DivResult LegNo="4" RunnerNo="1" /> 
    </Dividend> 
    </Pool> 
- <Pool PoolType="QN" Available="Y" Abandoned="N" PoolDisplayStatus="PAYING" PoolTotal="7029.00" JPotInGross="0.00" JPotOutGross="0.00" LastCalcTime="2012-05-16T16:36:57" CalcTime="2012-05-16T16:41:31" StatDiv="0"> 
- <Dividend DivId="62406785" DivAmount="68.5000"> 
    <DivResult LegNo="1" RunnerNo="5" /> 
    <DivResult LegNo="2" RunnerNo="7" /> 
    </Dividend> 
    </Pool> 
    </Race> 
    </Meeting> 
    </RaceDay> 

是否有人可以告訴我怎麼在我的XML文件的讀取和扁平化的數據,並將其插入到合適的表中的SQL數據庫。 在此先感謝。

+0

**你做了什麼?? ** –

回答

0

首先,你必須解析XML,並存儲到定製的C#對象,或者您可以將XML直接傳遞到你的存儲過程,並做在那裏編碼保存到DB中。

將xml傳遞給存儲過程並對其進行操作有點困難,所以我建議在C#中解析它,然後獲取自定義對象。一旦你得到它,你可以做任何你想做的事情。

這是我的文章,解釋到底你需要什麼。 Read XML file in ASP.NET(C#)

以下是解析XML文件並從中生成自定義C#對象的示例代碼。

public CatSubCatList GenerateCategoryListFromProductFeedXML() 
{ 
    string path = System.Web.HttpContext.Current.Server.MapPath(_xmlFilePath); 

    XDocument xDoc = XDocument.Load(path); 

    XElement xElement = XElement.Parse(xDoc.ToString()); 


    List<Category> lstCategory = xElement.Elements("Product").Select(d => new Category 
    { 
     Code = Convert.ToString(d.Element("CategoryCode").Value), 
     CategoryPath = d.Element("CategoryPath").Value, 
     Name = GetCateOrSubCategory(d.Element("CategoryPath").Value, 0), // Category 
     SubCategoryName = GetCateOrSubCategory(d.Element("CategoryPath").Value, 1) // Sub Category 
    }).GroupBy(x => new { x.Code, x.SubCategoryName }).Select(x => x.First()).ToList(); 

    CatSubCatList catSubCatList = GetFinalCategoryListFromXML(lstCategory); 

    return catSubCatList; 
} 
相關問題