C# 如何避免xmlns="&引用;在添加的XML元素中?

C# 如何避免xmlns="&引用;在添加的XML元素中?,c#,.net,xml,xml-namespaces,xmldocument,C#,.net,Xml,Xml Namespaces,Xmldocument,我有287个SSRS报告(XML文件),需要在其中添加一个参数。 他们都是这样开始的: <?xml version="1.0" encoding="utf-8"?> <Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdes

我有287个SSRS报告(XML文件),需要在其中添加一个参数。
他们都是这样开始的:

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSources>
<DataSource Name="COR_Basic">
<rd:DataSourceID>addde073-f37c-4b59-ae3a-25231ffc0ec6</rd:DataSourceID>
<DataSourceReference>COR_Basic</DataSourceReference>
</DataSource>
</DataSources>
<InteractiveHeight>29.7cm</InteractiveHeight>
<ReportParameters>
<ReportParameter Name="in_report_name">
<DataType>String</DataType>
<DefaultValue>
<Values>
<Value>=Globals!ReportName</Value>
</Values>
</DefaultValue>
<Prompt>Report Name</Prompt>
<Hidden>true</Hidden>
</ReportParameter>
<ReportParameter Name="in_mandant">
<DataType>String</DataType>
<DefaultValue>
<Values>
<Value>0</Value>
</Values>
</DefaultValue>
<Prompt>in_mandant</Prompt>
<Hidden>true</Hidden>
</ReportParameter>
如何避免使用空的xmlns属性?
(缺少将xml文件作为文本加载并执行string.replace)

如何避免使用空的xmlns属性

在正确的命名空间中创建元素。由于,您的所有元素都应该在
http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition
名称空间

您可以尝试插入片段,然后设置其
InnerXml
值——这可能会适当地应用命名空间默认值

然而,就我个人而言,我会使用LINQtoXML,这使得名称空间处理变得非常简单,然后以编程方式构造元素,而不是从文本解析它们。事实上,无论如何,我都会以编程方式构造元素,而不是设置
InnerXml
——这将使精确控制名称空间之类的东西变得更容易,因为您可以在构造元素时指定它们

(我还强烈建议使用
指令,以避免代码中到处都是完全限定名。)

如何避免使用空的xmlns属性

在正确的命名空间中创建元素。由于,您的所有元素都应该在
http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition
名称空间

您可以尝试插入片段,然后设置其
InnerXml
值——这可能会适当地应用命名空间默认值

然而,就我个人而言,我会使用LINQtoXML,这使得名称空间处理变得非常简单,然后以编程方式构造元素,而不是从文本解析它们。事实上,无论如何,我都会以编程方式构造元素,而不是设置
InnerXml
——这将使精确控制名称空间之类的东西变得更容易,因为您可以在构造元素时指定它们

(我还强烈建议使用
指令,以避免代码中到处都是完全限定名。)

如何避免使用空的xmlns属性

在正确的命名空间中创建元素。由于,您的所有元素都应该在
http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition
名称空间

您可以尝试插入片段,然后设置其
InnerXml
值——这可能会适当地应用命名空间默认值

然而,就我个人而言,我会使用LINQtoXML,这使得名称空间处理变得非常简单,然后以编程方式构造元素,而不是从文本解析它们。事实上,无论如何,我都会以编程方式构造元素,而不是设置
InnerXml
——这将使精确控制名称空间之类的东西变得更容易,因为您可以在构造元素时指定它们

(我还强烈建议使用
指令,以避免代码中到处都是完全限定名。)

如何避免使用空的xmlns属性

在正确的命名空间中创建元素。由于,您的所有元素都应该在
http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition
名称空间

您可以尝试插入片段,然后设置其
InnerXml
值——这可能会适当地应用命名空间默认值

然而,就我个人而言,我会使用LINQtoXML,这使得名称空间处理变得非常简单,然后以编程方式构造元素,而不是从文本解析它们。事实上,无论如何,我都会以编程方式构造元素,而不是设置
InnerXml
——这将使精确控制名称空间之类的东西变得更容易,因为您可以在构造元素时指定它们


(我还强烈建议使用
指令,以避免您的代码到处都是完全限定名。)

这似乎根本不可能(“感谢”只读属性)。
唯一的解决方法是替换文档的OuterXml,然后重新加载文档

    public static void SaveDocument(System.Xml.XmlDocument doc, string strFilename, bool bDoReplace)
    {
        string strSavePath = GetSavePath();
        strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFilename));

        if (bDoReplace)
        {
            doc.LoadXml(doc.OuterXml.Replace("xmlns=\"\"", ""));
            // doc.LoadXml(doc.OuterXml.Replace(strTextToReplace, strReplacementText));
        }

        using (System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(strSavePath, System.Text.Encoding.UTF8))
        {
            xtw.Formatting = System.Xml.Formatting.Indented; // if you want it indented
            xtw.Indentation = 4;
            xtw.IndentChar = ' ';

            doc.Save(xtw);
        } // End Using xtw

    } // End Sub SaveDocument

这似乎根本不可能(“感谢”只读属性)。
唯一的解决方法是替换文档的OuterXml,然后重新加载文档

    public static void SaveDocument(System.Xml.XmlDocument doc, string strFilename, bool bDoReplace)
    {
        string strSavePath = GetSavePath();
        strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFilename));

        if (bDoReplace)
        {
            doc.LoadXml(doc.OuterXml.Replace("xmlns=\"\"", ""));
            // doc.LoadXml(doc.OuterXml.Replace(strTextToReplace, strReplacementText));
        }

        using (System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(strSavePath, System.Text.Encoding.UTF8))
        {
            xtw.Formatting = System.Xml.Formatting.Indented; // if you want it indented
            xtw.Indentation = 4;
            xtw.IndentChar = ' ';

            doc.Save(xtw);
        } // End Using xtw

    } // End Sub SaveDocument

这似乎根本不可能(“感谢”只读属性)。
唯一的解决方法是替换文档的OuterXml,然后重新加载文档

    public static void SaveDocument(System.Xml.XmlDocument doc, string strFilename, bool bDoReplace)
    {
        string strSavePath = GetSavePath();
        strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFilename));

        if (bDoReplace)
        {
            doc.LoadXml(doc.OuterXml.Replace("xmlns=\"\"", ""));
            // doc.LoadXml(doc.OuterXml.Replace(strTextToReplace, strReplacementText));
        }

        using (System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(strSavePath, System.Text.Encoding.UTF8))
        {
            xtw.Formatting = System.Xml.Formatting.Indented; // if you want it indented
            xtw.Indentation = 4;
            xtw.IndentChar = ' ';

            doc.Save(xtw);
        } // End Using xtw

    } // End Sub SaveDocument

这似乎根本不可能(“感谢”只读属性)。
唯一的解决方法是替换文档的OuterXml,然后重新加载文档

    public static void SaveDocument(System.Xml.XmlDocument doc, string strFilename, bool bDoReplace)
    {
        string strSavePath = GetSavePath();
        strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFilename));

        if (bDoReplace)
        {
            doc.LoadXml(doc.OuterXml.Replace("xmlns=\"\"", ""));
            // doc.LoadXml(doc.OuterXml.Replace(strTextToReplace, strReplacementText));
        }

        using (System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(strSavePath, System.Text.Encoding.UTF8))
        {
            xtw.Formatting = System.Xml.Formatting.Indented; // if you want it indented
            xtw.Indentation = 4;
            xtw.IndentChar = ' ';

            doc.Save(xtw);
        } // End Using xtw

    } // End Sub SaveDocument

添加时,请使用具有以下设置的此命名空间

     XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
     namespaces.Add(string.Empty, string.Empty);

添加时,请使用具有以下设置的此命名空间

     XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
     namespaces.Add(string.Empty, string.Empty);

添加时,请使用具有以下设置的此命名空间

     XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
     namespaces.Add(string.Empty, string.Empty);

添加时,请使用具有以下设置的此命名空间

     XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
     namespaces.Add(string.Empty, string.Empty);

@进退两难:老鼠,没发现。在这种情况下,您可以在设置
InnerXml
片段之前尝试添加它,但老实说,您最好以编程方式创建它。这也行不通。不过还是谢谢你。顺便说一句,我不能用LINQ,我们还在.NET2.0上…@Quandary:Rats,还没发现。在这种情况下,您可以在设置
InnerXml
片段之前尝试添加它,但老实说,您最好以编程方式创建它。这也行不通。不过还是谢谢你。顺便说一句,我不能用LINQ,我们还在.NET2.0上…@Quandary:Rats,还没发现。在这种情况下,您可以在设置
InnerXml
片段之前尝试添加它,但老实说,您最好以编程方式创建它。这也行不通。但是