从 SQL 选择查询创建 XML
Posted
技术标签:
【中文标题】从 SQL 选择查询创建 XML【英文标题】:Create XML from SQL select query 【发布时间】:2021-01-21 10:59:34 【问题描述】:我正在尝试从 SQL 选择创建一个 xml,但我无法插入“:”(如 cac:PartyTaxScheme),也不能将 2 个数据放在一个元素中,请查看元素“cbc:CompanyID”(
<cac:PartyTaxScheme>
<cbc:RegistrationName>GRUPO FAM</cbc:RegistrationName>
<cbc:CompanyID schemeName="31" schemeID="0" schemeAgencyID="195">900711000</cbc:CompanyID>
<cbc:TaxLevelCode listName="48">O-23</cbc:TaxLevelCode>
<cac:RegistrationAddress>
<cbc:ID>11001</cbc:ID>
<cbc:CountrySubentityCode>11</cbc:CountrySubentityCode>
<cac:AddressLine>
<cbc:Line>CaLL 90</cbc:Line>
</cac:AddressLine>
<cac:Country>
<cbc:IdentificationCode>CO</cbc:IdentificationCode>
</cac:Country>
</cac:RegistrationAddress>
</cac:PartyTaxScheme>
非常感谢
【问题讨论】:
用您正在使用的数据库系统(例如 SQL Server、Oracle)和/或您正在使用的语言(如果您不使用 SQL 创建此结果)标记您的问题。跨度> 【参考方案1】:使用 Xml Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
class Program
static void Main(string[] args)
string ident = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<cac:PartyTaxScheme xmlns:cac=\"URL1\" xmlns:cbc=\"URL2\">" +
"</cac:PartyTaxScheme>";
XDocument doc = XDocument.Parse(ident);
string registrationName = "GPUPO FAM";
string schemeName = "31";
string schemeID = "0";
string schemeAgencyID = "195";
string companyID = "900711000";
string listName = "48";
string taxLevelCode = "0-23";
string ID = "11001";
string countrySubentityCode = "11";
string address = "CaLL 90";
string identificationCode = "CO";
XElement partyTaxScheme = doc.Root;
XNamespace nsCac = partyTaxScheme.GetNamespaceOfPrefix("cac");
XNamespace nsCbc = partyTaxScheme.GetNamespaceOfPrefix("cbc");
XElement xRegistrationName = new XElement(nsCac + "RegistrationName", registrationName);
partyTaxScheme.Add(xRegistrationName);
XElement xCompanyID = new XElement(nsCbc + "CompanyID", new object[]
new XAttribute("scnemeName", schemeName),
new XAttribute("schemeID", schemeID),
new XAttribute("schemeAgencyID", schemeAgencyID),
companyID
);
partyTaxScheme.Add(xCompanyID);
XElement xTaxLevelCode = new XElement(nsCbc + "TaxLevelCode", new object[]
new XAttribute("listName", listName),
taxLevelCode
);
partyTaxScheme.Add(xTaxLevelCode);
XElement xRegistrationAddress = new XElement(nsCac + "RegistrationAddress");
partyTaxScheme.Add(xRegistrationAddress);
xRegistrationAddress.Add(new XElement(nsCbc + "ID", ID));
xRegistrationAddress.Add(new XElement(nsCbc + "CountrySubentityCode", countrySubentityCode));
xRegistrationAddress.Add(new XElement(nsCac + "AddressLine", new XElement(nsCbc + "Line", address)));
xRegistrationAddress.Add(new XElement(nsCac + "Country", new XElement(nsCbc + "IdentificationCode", countrySubentityCode)));
【讨论】:
以上是关于从 SQL 选择查询创建 XML的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Pentaho-Kettle 中基于动态选择的 sql 查询中获取结果集?