标签: sql-server-openxml

SQL OpenXML多标记问题

我刚刚通过SQL Server Management Studio将XML文件读入表中.可能有更好的方法,但我想使用这种方法.

目前,我正在阅读人们记录的标准XML文件.一个<record>标签是每行数据的最高水平.我想将所有记录读入单独的行到我的SQL表中.

到目前为止,我已经使用以下方法进行了如下处理:

SELECT
        -- Record
        category, editor, entered, subcategory, uid, updated,
        -- Person
        first_name, last_name, ssn, ei, title, POSITION,
FROM OPENXML(@hDoc, 'records/record/person/names')
WITH 
(
    -- Record
    category [varchar](100) '../../@category',
    editor [varchar](100) '../../@editor',
    entered Datetime '../../@entered',
    subcategory [varchar](100) '../../@subcategory',
    uid BIGINT '../../@uid',
    updated [varchar](100) '../../@updated',
    -- Person
    first_name [varchar](100) 'first_name',
    last_name [varchar](100) 'last_name',
    ssn [varchar](100) '../@ssn',
    ei [varchar](100) '../@e-i',
    title [varchar](100) '../title',
    Position [varchar](100) '../position',  
)
Run Code Online (Sandbox Code Playgroud)

然而,这种方法运行良好,因为标签名称对于每个记录/人都是唯一的.我的问题是在<Person>标签内我现在有一个<Aliases>标签,其中包含一个以上<Alias> test name </Alias> …

xml sql sql-server openxml sql-server-openxml

16
推荐指数
1
解决办法
1365
查看次数

使用多个同名元素解析OpenXML

我有这样的数据结构:

<rootnode>
  <group>
    <id>1</id>
     <anothernode>first string</anothernode>
     <anothernode>second string</anothernode>
  </group>
 <group>
   <id>2</id>
     <anothernode>third string</anothernode>
     <anothernode>fourth string</anothernode>
  </group>
</rootnode>
Run Code Online (Sandbox Code Playgroud)

以下代码:

EXEC sp_xml_preparedocument @index OUTPUT, @XMLdoc

SELECT *
FROM OPENXML (@index, 'rootnode/group')
WITH 
(
  id int 'id',
  anothernode varchar(30) 'anothernode'
)
Run Code Online (Sandbox Code Playgroud)

这给了我结果

id | anothernode
————————————————
1  | first string
2  | third string
Run Code Online (Sandbox Code Playgroud)

如何在显示所有四个字符串的位置显示此结果?

id | anothernode
————————————————
1  | first string
1  | second string
2  | third string
2  | fourth string
Run Code Online (Sandbox Code Playgroud)

sql sql-server openxml sql-server-2008 sql-server-openxml

4
推荐指数
1
解决办法
8624
查看次数

sql 中 openxml 的动态列和记录

我有一个非常简单的 xml 文档。唯一的区别是元素可以改变。有一次我可能会:

<data><PersonalInfo>
<Person><FirstName>Bob</FirstName><LastName>Smith</LastName></Person>
<Person><FirstName>John</FirstName><LastName>Doe</LastName></Person>
</PersonalInfo></data>
Run Code Online (Sandbox Code Playgroud)

下次我可能会:

<data><AddressInfo>
<Address><City>Cleveland</City><State>OH</State></Address>
<Address><City>Chicago</City><State>IL</State></Address>
</AddressInfo></data>
Run Code Online (Sandbox Code Playgroud)

我想编写一个 select 语句,根据我目前拥有的 xml 文档生成一个动态表。

例如:对于第一个:

First Name     Last Name
------------------------
Bob             Smith
John            Doe
Etc...
Run Code Online (Sandbox Code Playgroud)

对于第二个

City       State
-----------------------
Cleveland   OH
Chicago     IL
Etc...
Run Code Online (Sandbox Code Playgroud)

这两个例子没有任何关系(鲍勃不是来自克利夫兰,等等......)

我只想使用相同的代码来生成两个表...取决于 xml 文档。当然,唯一的区别是节点引用:

Example 1:  data/PersonalInfo/Person*
Example 2:  data/AddressInfo/Address*
Run Code Online (Sandbox Code Playgroud)

我不想组合或更改 xml 文档结构中的任何内容。它们就是它们所进来的。我如何引用每一个来创建上面的两个不同的表 - 每个进来的 xml 文档都将在一个单独的运行存储过程中。但它将是相同的存储过程。非常感谢任何帮助,提前致谢!

xml sql-server sql-server-openxml

2
推荐指数
1
解决办法
2848
查看次数

使用OpenXML从HTML文件生成docx文件

我正在使用这种方法来生成docx文件:

public static void CreateDocument(string documentFileName, string text)
{
    using (WordprocessingDocument wordDoc =
        WordprocessingDocument.Create(documentFileName, WordprocessingDocumentType.Document))
    {
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

        string docXml =
                    @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
                 <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
                 <w:body><w:p><w:r><w:t>#REPLACE#</w:t></w:r></w:p></w:body>
                 </w:document>";

        docXml = docXml.Replace("#REPLACE#", text);

        using (Stream stream = mainPart.GetStream())
        {
            byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
            stream.Write(buf, 0, buf.Length);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力:

CreateDocument("test.docx", "Hello");
Run Code Online (Sandbox Code Playgroud)

但是,如果我想放置HTML内容而不是Hello?例如:

CreateDocument("test.docx", @"<html><head></head>
                              <body>
                                    <h1>Hello</h1>
                              </body>
                        </html>");
Run Code Online (Sandbox Code Playgroud)

或类似这样的东西:

CreateDocument("test.docx", @"Hello<BR>
                                    This is a simple text<BR>
                                    Third paragraph<BR>
                                    Sign
                        ");
Run Code Online (Sandbox Code Playgroud)

两种情况都为创建了无效的结构document.xml …

c# docx openxml sql-server-openxml

2
推荐指数
2
解决办法
6210
查看次数

标签 统计

sql-server-openxml ×4

openxml ×3

sql-server ×3

sql ×2

xml ×2

c# ×1

docx ×1

sql-server-2008 ×1