在节点上方添加子注释

时间:2019-12-19 20:41:21

标签: c# sql xml

我正试图从SQL转换为XML,但必须正确设置其格式,否则将无法正常工作。我正在使用C#在QBXML之间插入我的sql

select employee_name as Name ,'OCI Associates' as [CompanyName],'Mr' as Salutation ,
 LEFT(employee_name,CHARINDEX(' ',employee_name + ' ')-1) as FirstName,  REVERSE(LEFT(REVERSE(employee_name),
CHARINDEX(' ',REVERSE(employee_name))- 1)) AS LastName


  from EMPLOYEELIST 
for xml raw('CustomerAdd') , ROOT('CustomerAddRq'), ELEMENTS

这就是我得到的

<CustomerAdd>
        <Name>Zohreh FAKELASTNAME</Name>
        <Salutation>Mr</Salutation>
        <FirstName>Zohreh</FirstName>
        <LastName>FAKELASTNAME</LastName>
      </CustomerAdd>
      <CustomerAdd>
        <Name>Phillip FAKELASTNAME</Name>
        <Salutation>Mr</Salutation>
        <FirstName>Phillip</FirstName>
        <LastName>FAKELASTNAME</LastName>
      </CustomerAdd>

这就是我需要的

'''

 <CustomerAddRq>
<CustomerAdd>
        <Name>Zohreh FAKELASTNAME</Name>
        <Salutation>Mr</Salutation>
        <FirstName>Zohreh</FirstName>
        <LastName>FAKELASTNAME</LastName>
      </CustomerAdd>
 </CustomerAddRq>
 <CustomerAddRq>
  <CustomerAdd>
    <Name>Phillip FAKELASTNAME</Name>
    <Salutation>Mr</Salutation>
    <FirstName>Phillip</FirstName>
    <LastName>FAKELASTNAME</LastName>
  </CustomerAdd>

'''

我不确定如何在每个“ CustomerAdd”上添加“ CustomerAddRq”

2 个答案:

答案 0 :(得分:0)

尝试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[] xmlArray = {
                                    "<CustomerAdd>" +
                                      "<Name>Zohreh FAKELASTNAME</Name>" +
                                      "<Salutation>Mr</Salutation>" +
                                      "<FirstName>Zohreh</FirstName>" +
                                      "<LastName>FAKELASTNAME</LastName>" +
                                    "</CustomerAdd>",
                                    "<CustomerAdd>" +
                                      "<Name>Phillip FAKELASTNAME</Name>" +
                                      "<Salutation>Mr</Salutation>" +
                                      "<FirstName>Phillip</FirstName>" +
                                      "<LastName>FAKELASTNAME</LastName>" +
                                    "</CustomerAdd>"
                                };
            XElement customerAddRq = new XElement("CustomerAddRq");
            customerAddRq.Add(xmlArray.Select(x => XElement.Parse(x)));
        }
    }
}

答案 1 :(得分:0)

请-对于您的下一个问题-始终使用实际的RDBMS(供应商和版本)进行标记。您已经用[sql]标记了,这还不够...

从您的代码中,我认为这是SQL Server。

您可以尝试以下方法:

创建一个样机表来模拟您的问题(请您下次自行执行)

DECLARE @fakeTable TABLE(employee_name VARCHAR(100), salutation VARCHAR(50));
INSERT INTO @fakeTable VALUES
 ('Zohreh FAKELASTNAME','Mr')
,('One MORE','Ms')

-您自己的查询,稍有更改

select employee_name as [CustomerAdd/Name] 
      ,'OCI Associates' as [CustomerAdd/CompanyName]
      , salutation as [CustomerAdd/Salutation]
      , LEFT(employee_name,CHARINDEX(' ',employee_name + ' ')-1) as [CustomerAdd/FirstName]
      ,  REVERSE(LEFT(REVERSE(employee_name),CHARINDEX(' ',REVERSE(employee_name))- 1)) AS [CustomerAdd/LastName]
from @fakeTable EMPLOYEELIST 
for xml PATH('CustomerAddRq');

简而言之:

我使用PATH模式。这允许在列的别名中使用XPath表达式。
ROOT将是最外面的元素,仅出现一次。 PATH('ThisName')之后的名称将出现在任何行的周围。
现在神奇了:

  • 引擎打开该行的标签,并找到一个[CustomerAdd/Name]元素。
  • 它将先写入开始标签<CustomerAdd>,然后再写入开始标签<Name>
  • 将内容写入text()节点到此元素中。
  • 现在引擎找到了<CustomerAdd/CompanyName>
  • 引擎将关闭<Name>,但是-仍在<CustomerAdd>内-将在此处继续并打开<CompanyName>
  • ...等等...