我使用的XmlDocument和XmlNode的操作XML文件。说我想所谓的语言一个有效的节点添加到根,我用这样的code:
昏暗languageNode作为XmlNode的= xmlDoc.CreateNode(XmlNodeType.Element,语言,是Nothing)
languageNode.InnerText =EN-US
root.AppendChild(languageNode)
其中xmlDoc中是如下的对象和已经装载。然而,在手术后的XML文件,它appeas这样的:
<语言的xmlns => EN-US< /语言>
和这不通过验证。反正有摆脱命名空间?谢谢!
更新: 我编辑.rdlc文件,它定义了一个本地报表,并使用XML格式。部分文件看起来是这样的:
< XML版本=1.0编码=UTF-16&GT?;
<报告的xmlns =http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition
的xmlns:RD =http://schemas.microsoft.com/SQLServer/reporting/reportdesigner的xmlns:msxsl =瓮:架构 - 微软COM:XSLT的xmlns:XS =http://www.w3.org / 2001 / XML模式的xmlns:MSDATA =瓮:架构 - 微软COM:XML-MSDATA>
<语言的xmlns => EN-US< /语言>
...
一般情况下不应该有这么多的命名空间中使用,但我从XSLT生成它。但语言节点添加生成此文件之后。我的code是这样的:
昏暗xmlRdlc作为新的XmlDocument()
xmlRdlc.Load(文件)
昏暗的根作为XmlNode的= xmlRdlc.DocumentElement()
昏暗languageNode作为XmlNode的= xmlRdlc.CreateNode(XmlNodeType.Element,语言,是Nothing)
languageNode.InnerText =EN-US
root.AppendChild(languageNode)
xmlRdlc.Save(文件)
所以,我应该怎么做才能添加所需的节点是这样的:
<语言> EN-US< /语言>
解决方案
您需要指定命名空间。通过http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition作为最后一个参数的方法。
昏暗xmlRdlc作为新的XmlDocument()
xmlRdlc.Load(文件)
昏暗的根作为XmlNode的= xmlRdlc.DocumentElement()
昏暗languageNode作为XmlNode的= xmlRdlc.CreateNode(XmlNodeType.Element,语言,_
http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition)
languageNode.InnerText =EN-US
root.AppendChild(languageNode)
xmlRdlc.Save(文件)
顺便说一句,使用LINQ to XML,这将是更清洁的:
昏暗rdlcNS作为的XNamespace =http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition
昏暗xmlRdlc = XDocument.Load(文件)
xmlRdlc.Root.Add(新的XElement(rdlcNS +语言,EN-US))
xmlRdlc.Save(文件)
(转换到VB.NET是近似的,您的情况可能有所不同,等等。)
I am using XmlDocument and XmlNode to manipulate a xml file. Say I want to add a valid node called "Language" to the root, I use such code:
Dim languageNode As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "Language", Nothing)
languageNode.InnerText = "en-US"
root.AppendChild(languageNode)
where xmlDoc is a XmlDocument object and has already been loaded. However, in the xml file after the operation, it appeas like this:
<Language xmlns="">en-US</Language>
And this doesn't pass the validation. Is there anyway to get rid of the namespace? Thanks!
Update: I am editing a .rdlc file, which defines a local report, and using xml format. Part of the file looks like this:
<?xml version="1.0" encoding="utf-16"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"
xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<Language xmlns="">en-US</Language>
...
Normally there shouldn't be so many namespaces in use but I am generating it from xslt. But the Language node is added after this file is generated. My code looks like this:
Dim xmlRdlc As New XmlDocument()
xmlRdlc.Load(file)
Dim root As XmlNode = xmlRdlc.DocumentElement()
Dim languageNode As XmlNode = xmlRdlc.CreateNode(XmlNodeType.Element, "Language", Nothing)
languageNode.InnerText = "en-US"
root.AppendChild(languageNode)
xmlRdlc.Save(file)
So how should I do to add the desired node like this:
<Language>en-US</Language>
解决方案
You need to specify the namespace. Pass "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" as the last parameter to the method.
Dim xmlRdlc As New XmlDocument()
xmlRdlc.Load(file)
Dim root As XmlNode = xmlRdlc.DocumentElement()
Dim languageNode As XmlNode = xmlRdlc.CreateNode(XmlNodeType.Element, "Language", _
"http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition")
languageNode.InnerText = "en-US"
root.AppendChild(languageNode)
xmlRdlc.Save(file)
BTW, this would be cleaner using LINQ to XML:
Dim rdlcNS As XNamespace = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"
Dim xmlRdlc = XDocument.Load(file)
xmlRdlc.Root.Add(New XElement(rdlcNS + "Language", "en-US"))
xmlRdlc.Save(file)
(translation to VB.NET approximate, your mileage may vary, etc.)