【问题标题】:How to set namespace in xml attribute?如何在 xml 属性中设置命名空间?
【发布时间】:2014-04-16 07:31:25
【问题描述】:

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

<?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") 指定的行上遇到错误:The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/

我该怎么办?

【问题讨论】:

    标签: xml powershell xml-namespaces xmlnode


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 2012-08-01
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多