因爲RDL XML文件中使用了兩個命名空間:sql
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">sqlserver
一個默認命名空間:xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"測試
沒有前綴的元素屬於默認命名空間,如:<DataSets>,<DataSet Name="Data">,<DataField>item_no</DataField>,<TypeName>System.String</TypeName>等。spa
訪問時要使用XmlNamespaceManager,給這個默認命名空間起個別名,如:def.code
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("def", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");orm
SelectSingleNode時,也要加上這個def,如:root.SelectSingleNode(string.Format("def:DataSet[@Name='{0}']", dataSetName), nsmgr);server
與xeDataSet.SelectSingleNode(string.Format("def:Fields/def:Field[@Name='{0}']", parameterName), nsmgr);xml
-----------------blog
一個名爲"rd"的命名空間:xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"string
目前只有一個元素在使用:<rd:TypeName>System.String</rd:TypeName>
-------------
防止子元素出現 xmlns="" 的狀況。在建立每一個元素時都要指定命名空間。
xmlDoc.CreateElement("TypeName", nspDef); 這個是默認命名空間的。
xmlDoc.CreateElement("rd", "TypeName", nspRd); 這個是rd命名空間的。
zzz
public static void UpDateClientLocalXml2(string rdlFilePath, string type, string dataSetName, string parameterName, string parameterType) { //2014.05.28-30 Qingl 加入 報表文件升級的處理 //測試追加已打開的報表文件rdl文件是否存在該節點字段 ,不存在則先追加 bool fieldIsFlag = true; //明細字段是否存在,false 表明不存在,true表明存在 if (string.IsNullOrEmpty(dataSetName)) dataSetName = "Data"; #region 方法2 //1.加載rdl的xml文件 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(rdlFilePath); //<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" //xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"> //2.默認命名空間 XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("def", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"); string nspDef = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"; string nspRd = "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"; if (type == "Fields") { //["DataSets"]["DataSet"]["Fields"] XmlElement root = xmlDoc.DocumentElement["DataSets"]; XmlElement xeDataSet = (XmlElement)root.SelectSingleNode(string.Format("def:DataSet[@Name='{0}']", dataSetName), nsmgr); if (xeDataSet == null) { /* <DataSet Name="DataM"> <Query> <DataSourceName> </DataSourceName> <CommandText> </CommandText> </Query> <Fields> <Field Name="zz"> <DataField>zz</DataField> <TypeName>System.String</TypeName> </Field> </Fields> </DataSet> */ XmlElement xenDataSet = xmlDoc.CreateElement("DataSet", nspDef); xenDataSet.SetAttribute("Name",dataSetName); XmlElement xenQuery = xmlDoc.CreateElement("Query", nspDef); XmlElement xenDataSourceName = xmlDoc.CreateElement("DataSourceName", nspDef); XmlElement xenCommandText = xmlDoc.CreateElement("CommandText", nspDef); xenQuery.AppendChild(xenDataSourceName); xenQuery.AppendChild(xenCommandText); XmlElement xenFields = xmlDoc.CreateElement("Fields", nspDef); XmlElement xenField = xmlDoc.CreateElement("Field", nspDef); xenField.SetAttribute("Name", parameterName); XmlElement xenDataField = xmlDoc.CreateElement("DataField", nspDef); xenDataField.InnerXml = parameterName; XmlElement xenTypeName = null; if (dataSetName == "Data") xenTypeName = xmlDoc.CreateElement("rd", "TypeName", nspRd); //名爲"Data"的數據集用的是有前綴的命名空間 else xenTypeName = xmlDoc.CreateElement("TypeName", nspDef); xenTypeName.InnerXml = parameterType;//"System.String"; xenField.AppendChild(xenDataField); xenField.AppendChild(xenTypeName); xenFields.AppendChild(xenField); xenDataSet.AppendChild(xenQuery); xenDataSet.AppendChild(xenFields); root.AppendChild(xenDataSet); } else { XmlElement xeFields = (XmlElement)xeDataSet.SelectSingleNode(string.Format("def:Fields"), nsmgr); XmlElement book1 = (XmlElement)xeFields.SelectSingleNode(string.Format("def:Field[@Name='{0}']", parameterName), nsmgr); //DataSet Field 部分的處理 if (book1 == null) { book1 = xmlDoc.CreateElement("Field", nspDef); book1.SetAttribute("Name", parameterName); XmlElement objNodeChild = xmlDoc.CreateElement("DataField", nspDef); objNodeChild.InnerXml = parameterName; book1.AppendChild(objNodeChild); XmlElement objNodeChildTypeName = null; if (dataSetName == "Data") objNodeChildTypeName = xmlDoc.CreateElement("rd", "TypeName", nspRd); //名爲"Data"的數據集用的是有前綴的命名空間 else objNodeChildTypeName = xmlDoc.CreateElement("TypeName", nspDef); objNodeChildTypeName.InnerXml = parameterType;//"System.String"; book1.AppendChild(objNodeChildTypeName); xeFields.AppendChild(book1); } } } //4.保存文件 xmlDoc.Save(rdlFilePath); #endregion }
zzz