SQL到XML文档

SQL到XML文档,sql,xml,visual-studio,xmldocument,Sql,Xml,Visual Studio,Xmldocument,我在Visual Studio 2013中的一个控制台应用程序中工作,我正在尝试将数据集中的SQL转换为XMLDocument。这是迄今为止我从XML输出中得到的: <?xml version="1.0" encoding="UTF-8"?> <ActvtyData schemaVersion="1.0" xmlns="ActivityData"> <Accts xmlns=""> <Acct> <AcctID>123</A

我在Visual Studio 2013中的一个控制台应用程序中工作,我正在尝试将数据集中的SQL转换为XMLDocument。这是迄今为止我从XML输出中得到的:

<?xml version="1.0" encoding="UTF-8"?>
<ActvtyData schemaVersion="1.0" xmlns="ActivityData">
<Accts xmlns="">
 <Acct>
  <AcctID>123</AcctID>
  <Actns>
    <Actn>
      <Tp>UNA</Tp>
      <Actvts>
        <Actvty>
          <Cd>LOA</Cd>
        </Actvty>
      </Actvts>
    </Actn>
  </Actns>
  <Actns>
    <Actn>
      <Tp>UNA</Tp>
      <Actvts>
        <Actvty>
          <Cd>MLP</Cd>
        </Actvty>
      </Actvts>
    </Actn>
  </Actns>
  <AcctID>12345</AcctID>
  <Actns>
    <Actn>
      <Tp>UNA</Tp>
      <Actvts>
        <Actvty>
          <Cd>LOA</Cd>
        </Actvty>
      </Actvts>
    </Actn>
  </Actns>
 </Acct>
</Accts>
</ActvtyData>
这就是我希望输出的结果:

<?xml version="1.0" encoding="UTF-8"?>
<ActvtyData schemaVersion="1.0" xmlns="ActivityData">
  <Accts xmlns="">
<Acct>
  <AcctID>123</AcctID>
  <Actns>
    <Actn>
      <Tp>UNA</Tp>
      <Actvts>
        <Actvty>
          <Cd>LOA</Cd>
        </Actvty>
        <Actvty>
          <Cd>MLP</Cd>
        </Actvty>
      </Actvts>
    </Actn>
  </Actns>
</Acct>
<Acct>
  <AcctID>12345</AcctID>
  <Actns>
    <Actn>
      <Tp>UNA</Tp>
      <Actvts>
        <Actvty>
          <Cd>LOA</Cd>
        </Actvty>
      </Actvts>
    </Actn>
   </Actns>
  </Acct>
 </Accts>
</ActvtyData>
这是我的方法。我知道这不是最有效的代码,但它现在应该是这样工作的,这是我第一次使用XML,我需要使用XMLDocument来完成:

     public static void CreateXML(DataSet ds)
     {
         int RecordTracker; //track current dataset record
         XmlNode accountNode = null;

         RecordTracker = 0;

         try
         {
             Boolean EndOfData;
             EndOfData = false;
             XmlDocument XML;
             XML = new XmlDocument();
             XmlNode XMLnode;
             XMLnode = XML.CreateXmlDeclaration("1.0", "UTF-8", null);
             XML.AppendChild(XMLnode);

             //Create the header elements and attributes
             XmlNode ActvtyDataNode = XML.CreateElement("ActvtyData");
             XmlAttribute ActvtyDataAttribute = XML.CreateAttribute("schemaVersion");
             ActvtyDataAttribute.Value = "1.0";
             ActvtyDataNode.Attributes.Append(ActvtyDataAttribute);
             ActvtyDataAttribute = XML.CreateAttribute("xmlns");
             ActvtyDataAttribute.Value = "ActivityData";
             ActvtyDataNode.Attributes.Append(ActvtyDataAttribute);
             XML.AppendChild(ActvtyDataNode);

             //Create the account elements and attributes (Accts xmlns="")
             XmlNode AcctsxmlnsNode = XML.CreateElement("Accts");
             XmlAttribute AcctsxmlnsAttribute = XML.CreateAttribute("xmlns");
             AcctsxmlnsNode.Attributes.Append(AcctsxmlnsAttribute);
             ActvtyDataNode.AppendChild(AcctsxmlnsNode);

             //Create the individual account element (Acct)
             XmlNode AcctxmlnsNode = XML.CreateElement("Acct");
             AcctsxmlnsNode.AppendChild(AcctxmlnsNode);

             //Set count to 0 sop it stops then there are no more accounts to cycle through
             //int intCountAccounts = 0;
             string strAccountNo = "";
             string strActionCodes = "";
             string strActvtyCodes = "";
             string strIAndECodes = "";

             //int intCurrentRow = 0;
             //No. of times to perform the loop

             accountNode = XML.CreateElement("AcctID");

             /// Main Account ID loop
             for (int intCountAccounts = 0; intCountAccounts < ds.Tables[0].Rows.Count; intCountAccounts++)
             {

                 if (strAccountNo != ds.Tables[0].Rows[RecordTracker]["AcctID"].ToString())
                 {
                     accountNode = XML.CreateElement("AcctID");
                     accountNode.InnerText = ds.Tables[0].Rows[RecordTracker]["AcctID"].ToString();
                     //accountNode.AppendChild(XML.CreateTextNode(ds.Tables[0].Rows[intCountAccounts]["AcctID"].ToString()));
                     AcctxmlnsNode.AppendChild(accountNode);                  

                     strAccountNo = ds.Tables[0].Rows[RecordTracker]["AcctID"].ToString();
                 }                 


                     //Action codes
                     strActionCodes = "";
                     //Activity codes
                     strActvtyCodes = "";
                     strIAndECodes = "";
                     while (strAccountNo == ds.Tables[0].Rows[RecordTracker]["AcctID"].ToString())
                     {
                         XmlElement ActnsNode = XML.CreateElement("Actns");
                         XmlElement ActnNode = XML.CreateElement("Actn");




                         if (strActionCodes != ds.Tables[0].Rows[RecordTracker]["Tp"].ToString())
                         {
                             //Create Actns element

                             AcctxmlnsNode.AppendChild(ActnsNode);

                             //Create Actn element
                             ActnsNode.AppendChild(ActnNode);




                             XmlNode actnNode = XML.CreateElement("Tp");

                             actnNode.InnerText = ds.Tables[0].Rows[RecordTracker]["Tp"].ToString();
                             ActnNode.AppendChild(actnNode);

                             ////Check until match is found then move onto next
                             strActionCodes = ds.Tables[0].Rows[RecordTracker]["Tp"].ToString();

                         }

                         XmlElement ActvtyNode = XML.CreateElement("Actvty");
                         XmlElement ActvtsNode = XML.CreateElement("Actvts");
                         //Loop through for all activities that do not match
                         if (strActvtyCodes != ds.Tables[0].Rows[RecordTracker]["Cd"].ToString())
                         {


                                 ActnNode.AppendChild(ActvtsNode);

                                 ActvtsNode.AppendChild(ActvtyNode);


                                 XmlNode actvtyNode = XML.CreateElement("Cd");
                                 actvtyNode.InnerText = ds.Tables[0].Rows[RecordTracker]["Cd"].ToString();
                                 ActvtyNode.AppendChild(actvtyNode);

                                 ////Check until match is found then move onto next
                                 strActvtyCodes = ds.Tables[0].Rows[RecordTracker]["Cd"].ToString();
                             }

                             //increment to next record in data set
                             RecordTracker++;
                             if (RecordTracker > intCountAccounts)
                             { break; }
                         }







                 }
             //Save as XML to file location
             XML.Save(@"C:\Users\JMERRY\ActivityFile.xml");
             }


         catch (Exception e)
         {
             Console.WriteLine(e.Message);

             //Environment.Exit(0);
             Console.ReadLine();
         }
    }

什么样的SQL?例如,SQL Server针对xmlSQL Server,我尝试过xml,但发现它不符合我的需要。您尝试过xml路径吗?是的,但以数据库表的设置方式,我无法获得所需的输出