How to set namespace in xml attribute?

廉价感情. 提交于 2019-12-24 12:04:49

问题


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>

My script to generate is:

 $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()

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/

What should I do?


回答1:


Pass the xmlns namespace instead of $null:

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


来源:https://stackoverflow.com/questions/23102791/how-to-set-namespace-in-xml-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!