如何防止 MSXML 将其自己的命名空间添加到我的 XML base64 编码文档中?

发布于 2024-09-19 10:08:19 字数 1561 浏览 2 评论 0原文

我正在对文档进行编码,以将其作为 base64 编码元素附加到 xml 文档中进行传输。这很简单,我只需将整个文件放入字节数组中,然后在将数据放入元素中时使用 MSXML 的 nodeTypedValue 对数据进行 Base64 编码。然而,问题在于 MS XML 随后将其自己的命名空间和数据类型属性添加到元素中,从而导致 xml 输出无法通过验证。我正在使用的代码以及输出如下。

如何抑制附加到元素的“xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64"”属性?

Private Function xmlBinaryDocument(filePath As String) As IXMLDOMElement
    Dim xmlDOM As MSXML2.DOMDocument60
    Set xmlDOM = New MSXML2.DOMDocument60

    Set xmlBinaryDocument = xmlDOM.createNode(NODE_ELEMENT, "document", NS)

    Dim strExtension As String
    Dim strMimeType As String
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject

    ' Info about file
    strExtension = fso.GetExtensionName(filePath)
    strMimeType = extToMime(strExtension)

    ' Now read the file as binary data into a byte array
    Dim file() As Byte
    file = GetFileBytes(filePath)

    ' Store it in the xml element as a base64 datatype
    xmlBinaryDocument.dataType = "bin.base64"
    xmlBinaryDocument.nodeTypedValue = file

    ' attributes
    xmlBinaryDocument.setAttribute "document_content_type", strMimeType 
    xmlBinaryDocument.setAttribute "document_encoding_type", "base64"
    xmlBinaryDocument.setAttribute "document_extension", strExtension
End Function

输出:

<document xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64" 
document_content_type="application/rtf" document_encoding_type="base64" 
document_extension="rtf">***base64 encoded file here***</document>

I'm encoding a document to attach as a base64 encoded element inside an xml document for transmission. It's easy enough, I just slurp the entire file into a byte array and then use MSXML's nodeTypedValue to base64 encode the data as I put it into the element. The problem, however, is that MS XML then adds its own namespace and datatype attributes into the element, making the xml output fail validation. The code I'm using is below as well as the output.

How do I suppress the "xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64"" attributes being appended to the element?

Private Function xmlBinaryDocument(filePath As String) As IXMLDOMElement
    Dim xmlDOM As MSXML2.DOMDocument60
    Set xmlDOM = New MSXML2.DOMDocument60

    Set xmlBinaryDocument = xmlDOM.createNode(NODE_ELEMENT, "document", NS)

    Dim strExtension As String
    Dim strMimeType As String
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject

    ' Info about file
    strExtension = fso.GetExtensionName(filePath)
    strMimeType = extToMime(strExtension)

    ' Now read the file as binary data into a byte array
    Dim file() As Byte
    file = GetFileBytes(filePath)

    ' Store it in the xml element as a base64 datatype
    xmlBinaryDocument.dataType = "bin.base64"
    xmlBinaryDocument.nodeTypedValue = file

    ' attributes
    xmlBinaryDocument.setAttribute "document_content_type", strMimeType 
    xmlBinaryDocument.setAttribute "document_encoding_type", "base64"
    xmlBinaryDocument.setAttribute "document_extension", strExtension
End Function

Output:

<document xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64" 
document_content_type="application/rtf" document_encoding_type="base64" 
document_extension="rtf">***base64 encoded file here***</document>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-09-26 10:08:19

应该在当地人中多挖掘一下:

xmlBinaryDocument.removeAttribute "dt:dt"

Should have dug around in Locals a bit more:

xmlBinaryDocument.removeAttribute "dt:dt"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文