【问题标题】:Sql XML to DelphiSql XML 到 Delphi
【发布时间】:2016-12-05 10:03:54
【问题描述】:

早上好,对于那些还在早上的人..

我有一个项目要做,我有以下内容: 我有一个有 4 列的 SQL TABLE,我们称它们为 C1,C2,C3,C4,这些列有一些我需要 Delphi 的数据,从这里我需要使用这个参数调用一个 C# 函数,例如:SQL TABLE XML -> DELPHI -> C#function(c1,c2,c3,c4)

我不知道如何将 XML 解析为 Delphi....

SELECT * FROM ExportModelMacheta FOR XML AUTO

我使用它,但我不知道之后我应该做什么,我在一个新的 sql 窗口中有 xml,但我想如何解析它。我应该使用TXMLDOCUMENT 吗?但这具有仅获取文件路径的参数,那么我该如何给他xml?我在想我应该在Delphi中进行一个查询,在其中我会调用这个选择并将所有内容保存在一个字符串中,然后在那个字符串中查找我想要的内容并提取它们,但是我猜还有很多工作要做应该有更好的方法来实现这一点。 如果我有一个文件来提供路径会很容易,但我被告知我需要从 sql 中制作所有内容......

这是我应该在 delphi 中获取的 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" />

【问题讨论】:

  • Delphi 可以很好地直接与 SQL Server 对话,为什么还要通过 xml?

标签: c# sql-server xml delphi


【解决方案1】:

这样的事情怎么样

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;
        }
    }
}

【讨论】:

    【解决方案2】:

    我认为提供一个 Delphi 示例来说明如何解析您获得的 XML 可能会有所帮助 从您的查询。但是,从这个 q 可以看出, How to Decode XML Blob field in D7 我在解码返回 XML 的 blob 字段的内容时遇到了问题。

    因此,下面的代码实现了一种解决方法,以将 XML 作为字符串而不是作为 blob。 然后它使用 MSXML xml 解析器将 XML 字符串解析为数据行、字段名和 字段值。

    注意:这是为 Delphi 7 编写的,当没有具体说明时,我倾向于将其用于 SO 答案 指定了 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;
    

    【讨论】:

      【解决方案3】:

      我解决了这个问题,我将我的 XML 导出到一个字符串中的 delphi 中,然后我使用那里的参数来调用我的 C# 函数。 谢谢你的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        相关资源
        最近更新 更多