1. 一個帶命名空間的XML如下
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<AutoRefresh>0</AutoRefresh>
<DataSources>
<DataSource Name="db_EMISDataSet">
2. 增加一個節點及屬性,以下是部分代碼:
xmldoc.Load(reader);
reader.Close();
namespaceManager = new XmlNamespaceManager(xmldoc.NameTable);//有命名空間的XML檔案需要這幾句!
namespaceManager.AddNamespace("A", "http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition");//預設的命名空間也要添加上去
namespaceManager.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
rootXE = xmldoc.DocumentElement; // DocumentElement 獲取xml檔案物件的根XmlElement.
private void addNode(string nodename,string location,string value)
{
XmlNode ItemND = rootXE.SelectSingleNode("/A:Report/A:ReportSections/A:ReportSection/A:Body/A:ReportItems", namespaceManager); //selectSingleNode 根據XPath運算式,獲得符合條件的第一個節點.
switch (nodename.Substring(0, 2))//txbJP01,JP01,Rectangle1,Line4
{
case "tx":
break;
case "JP":
break;
case "Re":
break;
case "Li":
XmlElement xeLine = xmldoc.CreateElement("Line");//創建Line節點
XmlAttribute xmlAttuName = xmldoc.CreateAttribute("Name");//創建屬性
xmlAttuName.InnerText = nodename;//屬性值,Line10開始
xeLine.SetAttributeNode(xmlAttuName);
3. 程式運行后,在Name屬性上自動添加了 xmlns="",怎么讓程式不增加這個命名空間?
</Style>
</Textbox>
<Line Name="Line10" xmlns="">
<Top>120.000cm</Top>
<Left>16.75458cm</Left>
<Height>0cm</Height>
<Width>2.70933cm</Width>
<Zindex>21</Zindex>
<Style>
<Border>
<Style>Solid</Style>
</Border>
</Style>
</Line>
</ReportItems>
<Height>11.69in</Height>
<Style />
</Body>
<Width>8.27in</Width>
<Page>
uj5u.com熱心網友回復:
找到答案了!以后各位遇到了可以參考!//1): 當我們在AppendChild的時候,如果ParentNode是帶有xmlns屬性的,而你新加的節點,在創建時不指定xmlns或指定為空時,子節點將出現xmlns = "" 屬性,終無法去掉。
//2): 當父節點具有xmlns屬性時,子節點必須指定xmlns屬性,但是當子節點的xmlns屬性與父節點命名空間相同時,子節點不顯示xmlns屬性。
例如: XmlElement xeLine = xmldoc.CreateElement("Line", this.xmldoc.DocumentElement.NamespaceURI);//創建Line節點
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/7032.html
標籤:其他開發語言
上一篇:求助:誰先倒
