2016-12-05 103 views
-4

早上好,對於那些誰仍然是早上..SQL XML德爾福

我有一個項目做,我有以下幾點: 我有一個SQL TABLE有4列,讓我們給他們打電話C1,C2,C3,C4,這列有一些數據,我需要德爾福,從這裏我需要調用這個參數的前一個C#函數:SQL TABLE XML -> DELPHI -> C#function(c1,c2,c3,c4)

我不知道如何解析XML德爾福....

SELECT * FROM ExportModelMacheta FOR XML AUTO 

我用這個但是我不知道我應該做什麼後,我有一個新的SQL窗口中的XML,但我想如何解析它。我應該使用TXMLDOCUMENT嗎?但這有參數只能得到一個文件路徑,所以我怎麼想給他的xml?我在想,我應該在Delphi中做一個查詢,在這個查詢中,我會調用這個select並將所有內容保存在一個字符串中,然後查看該字符串中我想要的內容並提取它們,但是需要做很多工作,我猜應該有更好的方法來實現這一點。 這將是容易的,如果我有一個文件給路徑,但我被告知,我需要做的一切從SQL ...

這是xml代碼,我應該得到在德爾福爲了打電話給我C#功能

<ExportModelMacheta Macheta="ufImportProduesPrioritateXContactare" NumeColoana="IdArticol" TipDeDate="string" Pozitie="1" FromatMacheta="xls" /> 
<ExportModelMacheta Macheta="ufImportProduesPrioritateXContactare" NumeColoana="Prioritate" TipDeDate="string" Pozitie="2" FromatMacheta="xls" /> 
<ExportModelMacheta Macheta="ufImportExcelCaracteristiciUtilizatorXContactare" NumeColoana="IdUtilizator" TipDeDate="string" Pozitie="1" FromatMacheta="xls" /> 
<ExportModelMacheta Macheta="ufImportExcelCaracteristiciUtilizatorXContactare" NumeColoana="IdLocatie" TipDeDate="string" Pozitie="2" FromatMacheta="xls" /> 
<ExportModelMacheta Macheta="ufImportExcelCaracteristiciUtilizatorXContactare" NumeColoana="TipUtilizator" TipDeDate="string" Pozitie="3" FromatMacheta="xls" /> 
+1

爲什麼你想通過xml去當德爾福直接與SQL Server直接交談? –

回答

0

如何像這樣

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 


namespace ConsoleApplication29 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var ExportModelMachetas = new object[][] { 
       new object[] { "ufImportProduesPrioritateXContactare", "IdArticol", "string", "1"}, 
       new object[] { "ufImportProduesPrioritateXContactare", "Prioritate", "string", "2"}, 
       new object[] { "ufImportExcelCaracteristiciUtilizatorXContactare", "IdUtilizator", "string", "1"}, 
       new object[] { "ufImportExcelCaracteristiciUtilizatorXContactare", "IdLocatie", "string", "2"}, 
       new object[] { "ufImportExcelCaracteristiciUtilizatorXContactare", "TipUtilizator", "string", "3"} 
      }; 
      List<XElement> results = ToXML(ExportModelMachetas); 

     } 
     static List<XElement> ToXML(object[][] ExportModelMachetas) 
     { 
      List<XElement> results = new List<XElement>(); 

      foreach (var ExportModelMacheta in ExportModelMachetas) 
      { 
       XElement newExportModelMacheta = new XElement("ExportModelMacheta", new object[] { 
        new XAttribute("Macheta", ExportModelMacheta[0]), 
        new XAttribute("NumeColoana", ExportModelMacheta[2]), 
        new XAttribute("TipDeDate", ExportModelMacheta[2]), 
        new XAttribute("Pozitie", ExportModelMacheta[3]), 
        new XAttribute("FromatMacheta", "xls"), 
       }); 
       results.Add(newExportModelMacheta); 
      } 

      return results; 
     } 
    } 
} 
0

我想這可能有助於給如何分析你從查詢中獲取 的XML德爾福的例子。然而,正如你可以看到從這個q, How to Decode XML Blob field in D7 我碰到一個問題,解碼返回XML的blob字段的內容。

因此,下面的代碼實現了將XML作爲字符串而不是blob的解決方法。 然後,它使用MSXML xml解析器將XML字符串解析爲數據行,字段名稱和字段值。

注意:這是爲Delphi 7編寫的,當我沒有指定特定的 Delphi版本時,我傾向於使用它。它可能需要對Delphi和/或MSXML typelib導入單元的更高版本進行較小版本的更改。

另請注意,要使用此代碼,您不應在AdoQuery上定義持久性字段,因爲代碼會修改返回的數據的字段類型。

代碼

Uses [...], MSXML; 

procedure TForm1.FormCreate(Sender: TObject); 
var 
    SS : TStringStream; 
    MS : TMemoryStream; 
    Output : AnsiString; 
    i, 
    j : Integer; 
    XmlDoc: IXMLDOMDocument; 
    NodeList : IXmlDOMNodeList; 
    Attributes : IXMLDOMNamedNodeMap; 
    AttrNode : IXmlDomNode; 
begin 
    SS := TStringStream.Create(''); 
    MS := TMemoryStream.Create; 
    try 
    // the following line is to work around a problem decoding the blob field 
    // which you get when the query returms the XML as a blob 
    AdoQuery1.SQL.Text := 'select Convert(Text, (' + AdoQuery1.SQL.Text + '))'; 
    AdoQuery1.Open; 
    TBlobField(AdoQuery1.Fields[0]).SaveToStream(SS); 

    Output := SS.DataString; 
    Memo1.Lines.Text := Output; 

    // As can be seen from the contents of Memo1, the contents of Output are not 
    // well-formed XML, just a series of nodes with the same name. So, we surround them 
    // with a root node to make Output XML-parseable 
    Output := '<data>' + Output + '</data>'; 

    XmlDoc := CoDOMDocument.Create; 
    XmlDoc.Async := False; 
    XmlDoc.LoadXml(Output); 

    // The following XPath query gets the data-row nodes of the XML 
    NodeList := XmlDoc.documentElement.SelectNodes('/data/*'); 
    Assert(NodeList <> Nil); 

    Memo1.Lines.BeginUpdate; 
    Memo1.Lines.Clear; 
    for i := 0 to NodeList.Length - 1 do begin 
     Memo1.Lines.Add('Row'); 
     Attributes := NodeList.item[I].Attributes; 
     for j := 0 to Attributes.length - 1 do begin 
     Memo1.Lines.Add('Field'); 
     AttrNode := Attributes.item[j]; 
     Memo1.Lines.Add(AttrNode.nodeName + ': ' + AttrNode.nodeValue); 
     Memo1.Lines.Add(''); 
     end; 
    end; 

    finally 
    Memo1.Lines.EndUpdate; 
    XmlDoc := Nil; 
    SS.Free; 
    MS.Free; 
    end; 
end; 
0

我解決了這個問題,我出口我的XML字符串中的德爾福,我使用parameteres從那裏打電話給我的C#功能。 謝謝你的幫助。