2013-05-27 2 views
-1

C#을 사용하여 다음 XML 파일을 가져올 수 있습니까? 데이터는 SQL Server에 있습니다.C#을 사용하여 다음 XML 생성

<Person HomeID="1"> 
    <Day ID="1"> 
    <String>I get up at 07:00</String> 
    <String>I have breakfast at 07:30</String> 
    <String>I go to office at 08:00</String> 
    <String>I have lunch at 13:00</String> 
    <String>I come back from office at 17:00</String> 
    <String>I have dinner at 19:00</String> 
    <String>I sleep at 21:30</String> 
    </Day> 
    <Day ID="2"> 
    <String>I get up at 08:00</String> 
    <String>I have breakfast at 08:30</String> 
    <String>I have lunch at 13:00</String> 
    <String>I have dinner at 20:00</String> 
    <String>I sleep at 23:00</String> 
    </Day> 
</Person> 
<Person HomeID="2"> 
    <Day ID="1"> 
    <String>I get up at 08:00</String> 
    <String>I have breakfast at 08:30</String> 
    <String>I go to office at 09:00</String> 
    <String>I have lunch at 13:00</String> 
    <String>I come back from office at 18:00</String> 
    <String>I have dinner at 20:00</String> 
    <String>I sleep at 22:00</String> 
    </Day> 
    <Day ID="2"> 
    <String>I get up at 09:00</String> 
    <String>I have breakfast at 10:00</String> 
    <String>I have lunch at 13:00</String> 
    <String>I have dinner at 19:00</String> 
    <String>I sleep at 22:30</String> 
    </Day> 
</Person> 

나는 두 테이블, TB1 및 TB2 있습니다.

TB1의 필드는 HomeID, DayID, TimeCode, timevalue입니다.

HomeID DayID TimeCode timevalue 
    1  1  1   07:00:00 
    1  1  2   07:30:00 
    1  1  3   08:00:00 
    1  1  4   13:00:00 
    1  1  5   17:00:00 
    1  1  6   19:00:00 
    1  1  7   21:30:00 
    1  2  1   08:00:00 
    1  2  2   08:30:00 
    1  2  3   13:00:00 
    1  2  4   20:00:00 
    1  2  5   23:00:00 
    2  1  1   08:00:00 
    2  1  2   08:30:00 
    2  1  3   09:00:00 
    2  1  4   13:00:00 
    2  1  5   18:00:00 
    2  1  6   20:00:00 
    2  1  7   22:00:00 
    2  2  1   09:00:00 
    2  2  2   10:00:00 
    2  2  3   13:00:00 
    2  2  4   19:00:00 
    2  2  5   22:30:00 

TB2의 필드는 DayType, StringCode, RndString입니다.

DayType  StringCode RndString 
    1  1   I get up at 
    1  2   I have breakfast at 
    1  3   I go to office at 
    1  4   I have lunch at 
    1  5   I come back from office at 
    1  6   I have dinner at 
    1  7   I sleep at 
    2  1   I get up at 
    2  2   I have breakfast at 
    2  3   I have lunch at 
    2  4   I have dinner at 
    2  5   I sleep at 

참고 : TB1.DayID = TB2.DayType 및 TB1.TimeCode는 = TB2.StringCode 원하는 XML 출력을 생성하는 해당 SQL 쿼리는

select T1_1.HomeID as [@HomeID], 
     (
     select T1_2.DayID as [@ID], 
       (
       select T2.RndString+' '+left(T1_3.TimeValue, 5) as '*' 
       from TB1 as T1_3 
       inner join TB2 as T2 
        on T1_3.DayID = T2.DayType and 
        T1_3.TimeCode = T2.StringCode 
       where T1_2.HomeID = T1_3.HomeID and 
        T1_2.DayID = T1_3.DayID 
       order by T2.StringCode 
       for xml path('String'), type 
      ) 
     from TB1 as T1_2 
     where T1_2.HomeID = T1_1.HomeID 
     group by T1_2.DayID, 
       T1_2.HomeID 
     order by T1_2.DayID 
     for xml path('Day'), type 
     ) 
from TB1 as T1_1 
group by T1_1.HomeID 
order by T1_1.HomeID 
for xml path('Person') 

입니다

내 솔루션 시용.

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Windows.Forms; 
    using System.Text; 
    using System.Data; 
    using System.Data.SqlClient; 

    namespace SQL 
    { 
     static class Program 
     { 
      // To generate XML File using C# from SQL Server Data 

      // The main entry point for the application. 
      [STAThread] 
      static void Main() 
      { 
       // Create a String to hold the database connection string. 
       string sdwConnectionString = @"Data Source=IE1ADTBD5ZL1S\;Initial Catalog=RecommendEngine;Integrated Security=True"; 

       // Create a connection 
       SqlConnection sdwDBConnection = new SqlConnection(sdwConnectionString); 

       // Open the connection 
       sdwDBConnection.Open(); 



       //Create a String to hold the query. 
       string query = @"select T1_1.HomeID as [@HomeID], 
                (
                 select T1_2.DayID as [@ID], 
                   (
                   select T2.RndString+' '+left(T1_3.TimeValue, 5) as '*' 
                   from TB1 as T1_3 
                   inner join TB2 as T2 
                    on T1_3.DayID = T2.DayType and 
                     T1_3.TimeCode = T2.StringCode 
                   where T1_2.HomeID = T1_3.HomeID and 
                    T1_2.DayID = T1_3.DayID 
                   order by T2.StringCode 
                   for xml path('String'), type 
                  ) 
                 from TB1 as T1_2 
                 where T1_2.HomeID = T1_1.HomeID 
                 group by T1_2.DayID, 
                   T1_2.HomeID 
                 order by T1_2.DayID 
                 for xml path('Day'), type 
                ) 
                from TB1 as T1_1 
                group by T1_1.HomeID 
                order by T1_1.HomeID 
                for xml path('Person'), root('Persons')"; 

       //Create a SqlCommand object and pass the constructor the connection string and the query string. 
       SqlCommand queryCommand = new SqlCommand(query, sdwDBConnection); 

       DataTable dt = new DataTable(); 
       new SqlDataAdapter(queryCommand).Fill(dt); 
       dt.TableName = "TableName"; 
       dt.WriteXml(@"C:/SampleFile.xml"); 


       // Close the connection 
       sdwDBConnection.Close(); 

      } 
     } 
    } 
+0

-1. –

+0

어이 나는이 문제를 해결하려고 노력했다. 그러나 그것은 효과적으로 나오지 않았다. – SarangArd

답변

0

데이터에서 XML을 만들고 싶습니까? 그렇다면, 이것은 당신이

public XDocument CreateXML(string[][] activities) 
    { 
     int HomeID = 1; 
     XDocument doc = new XDocument(); 
     doc.Add(new XAttribute("HomeID", ""+HomeID++); 

     foreach (string[] dayactivities in activities) 
     { 
      int ID=1; 
      XElement day = new XElement("Day"); 
      day.Add(new XAttribute("ID", ""+ID++)); 
      doc.Add(day); 

      foreach (string s in dayactivities) 
       day.Add(new XElement("String", s)); 
     } 

     return doc; 
    } 
1

이 하나의 시도 시작하는 것입니다 -

스키마 :

SET NOCOUNT ON; 

DECLARE @TB1 TABLE 
(
     HomeID INT 
     , DayID INT 
     , TimeCode INT 
     , timevalue CHAR(10) 
) 

INSERT INTO @TB1 (HomeID, DayID, TimeCode, timevalue) 
VALUES 
    (1, 1, 1, '07:00:00'), (1, 1, 2, '07:30:00'), 
    (1, 1, 3, '08:00:00'), (1, 1, 4, '13:00:00'), 
    (1, 1, 5, '17:00:00'), (1, 1, 6, '19:00:00'), 
    (1, 1, 7, '21:30:00'), (1, 2, 1, '08:00:00'), 
    (1, 2, 2, '08:30:00'), (1, 2, 3, '13:00:00'), 
    (1, 2, 4, '20:00:00'), (1, 2, 5, '23:00:00'), 
    (2, 1, 1, '08:00:00'), (2, 1, 2, '08:30:00'), 
    (2, 1, 3, '09:00:00'), (2, 1, 4, '13:00:00'), 
    (2, 1, 5, '18:00:00'), (2, 1, 6, '20:00:00'), 
    (2, 1, 7, '22:00:00'), (2, 2, 1, '09:00:00'), 
    (2, 2, 2, '10:00:00'), (2, 2, 3, '13:00:00'), 
    (2, 2, 4, '19:00:00'), (2, 2, 5, '22:30:00') 

DECLARE @TB2 TABLE 
(
     DayType INT 
     , StringCode INT 
     , RndString VARCHAR(50) 
) 

INSERT INTO @TB2 (DayType, StringCode, RndString) 
VALUES 
    (1, 1, 'I get up at '), 
    (1, 2, 'I have breakfast at '), 
    (1, 3, 'I go to office at '), 
    (1, 4, 'I have lunch at '), 
    (1, 5, 'I come back from office at '), 
    (1, 6, 'I have dinner at '), 
    (1, 7, 'I sleep at '), 
    (2, 1, 'I get up at '), 
    (2, 2, 'I have breakfast at '), 
    (2, 3, 'I have lunch at '), 
    (2, 4, 'I have dinner at '), 
    (2, 5, 'I sleep at ') 

예 1 :

SELECT 
     '@HomeID' = t.HomeID 
    , (
      SELECT '@ID' = t2_.DayID 
       , (
        SELECT t4.RndString + LEFT(t3.TimeValue, 5) 
        FROM @TB1 t3 
        JOIN @TB2 t4 ON t3.DayID = t4.DayType AND t3.TimeCode = t4.StringCode 
        WHERE t2_.DayID = t3.DayID 
         AND t.HomeID = t3.HomeID 
        FOR XML PATH ('String'), TYPE 
       ) 
      FROM (
       SELECT DISTINCT t2.DayID 
       FROM @TB1 t2 
       WHERE t.HomeID = t2.HomeID 
      ) t2_ 
      FOR XML PATH ('Day'), TYPE 
     ) 
FROM (
    SELECT DISTINCT T1.HomeID 
    FROM @TB1 T1 
) t 
FOR XML PATH ('Person'), TYPE 

예 2 :

SELECT 
     Person.HomeID 
     , ID = [Day].DayID 
     , String.String 
FROM @TB1 Person 
JOIN (
    SELECT T1.HomeID, T1.DayID 
    FROM @TB1 T1  
) [Day] on Person.HomeID = [Day].HomeID 
JOIN (
    SELECT 
      String = t4.RndString + LEFT(t3.TimeValue, 5) 
     , t3.DayID 
     , t3.HomeID 
    FROM @TB1 t3 
    JOIN @TB2 t4 ON t3.DayID = t4.DayType AND t3.TimeCode = t4.StringCode 
) String ON [Day].DayID = String.DayID AND [Day].HomeID = String.HomeID 
GROUP BY Person.HomeID, [Day].DayID, String 
FOR XML AUTO 

출력 : 그것을 자신을 시도하는 어떤 노력을 보여주는하지 않는

<Person HomeID="1"> 
    <Day ID="1"> 
    <String>I get up at 07:00</String> 
    <String>I have breakfast at 07:30</String> 
    <String>I go to office at 08:00</String> 
    <String>I have lunch at 13:00</String> 
    <String>I come back from office at 17:00</String> 
    <String>I have dinner at 19:00</String> 
    <String>I sleep at 21:30</String> 
    </Day> 
    <Day ID="2"> 
    <String>I get up at 08:00</String> 
    <String>I have breakfast at 08:30</String> 
    <String>I have lunch at 13:00</String> 
    <String>I have dinner at 20:00</String> 
    <String>I sleep at 23:00</String> 
    </Day> 
</Person> 
<Person HomeID="2"> 
    <Day ID="1"> 
    <String>I get up at 08:00</String> 
    <String>I have breakfast at 08:30</String> 
    <String>I go to office at 09:00</String> 
    <String>I have lunch at 13:00</String> 
    <String>I come back from office at 18:00</String> 
    <String>I have dinner at 20:00</String> 
    <String>I sleep at 22:00</String> 
    </Day> 
    <Day ID="2"> 
    <String>I get up at 09:00</String> 
    <String>I have breakfast at 10:00</String> 
    <String>I have lunch at 13:00</String> 
    <String>I have dinner at 19:00</String> 
    <String>I sleep at 22:30</String> 
    </Day> 
</Person> 
+0

업데이트 된 답변을 시도하십시오. – Devart