如何在 xml 属性中设置命名空间? [英] How to set namespace in xml attribute?

查看:20
本文介绍了如何在 xml 属性中设置命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xml 文档,我想使用 powershell 脚本生成它,如下所示:

I have an xml document which is as below that I want to generate using powershell script:

<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:JansenTRAM:-myXSD-2013-06-27T10-48-27" solutionVersion="0.0.0.1471" productVersion="15.0.0.0" PIVersion="1.0.0.0" href="http:xxx"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<?mso-infoPath-file-attachment-present?>


<my:TemplateFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" >
.....
.....
</my:TemplateFields>

我要生成的脚本是:

 $filePath= "C:\Powershell\Report.xml"
if(Test-Path $filePath)
{
    Remove-Item $filePath
} 

#set encoding
$encoding = [System.Text.Encoding]::UTF8

# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$encoding)

# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"

# Write the XML Decleration
$xmlWriter.WriteStartDocument()

#SetNamespace($XmlWriter)

# Write Root Element
$xmlWriter.WriteStartElement($RootElement)
$XmlWriter.WriteAttributeString("xmlns","xsi", $null, "http://www.w3.org/2001/XMLSchema-instance")


#$xmlWriter.WriteEndElement # <-- Closing Servers
# $XmlWriter.


# Write Close Tag for Root Element
$xmlWriter.WriteEndElement # <-- Closing RootElement

# End the XML Document
$xmlWriter.WriteEndDocument()

# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()

我在 $XmlWriter.WriteAttributeString("xmlns","xsi", $null, "http://www.w3.org/2001/XMLSchema-instance") 行上遇到错误code> 指定:'xmlns' 属性绑定到保留的命名空间 'http://www.w3.org/2000/xmlns/

I am getting an error on the line $XmlWriter.WriteAttributeString("xmlns","xsi", $null, "http://www.w3.org/2001/XMLSchema-instance") specifying : The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/

我该怎么办?

推荐答案

传递 xmlns 命名空间而不是 $null:

Pass the xmlns namespace instead of $null:

$XmlWriter.WriteAttributeString("xmlns", "xsi", 
    "http://www.w3.org/2000/xmlns/", 
    "http://www.w3.org/2001/XMLSchema-instance");

这篇关于如何在 xml 属性中设置命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆