简体   繁体   English

如何在C#中的XML文档中添加名称空间

[英]how to add namespaces in XML doc in C#

This is the child node that I want to add but if I use createElement method it includes xmlns attribute automatically but it doesn't required so give me some solution to add below node in xml . 这是我要添加的子节点,但是如果我使用createElement方法,它会自动包含xmlns属性,但是它不是必需的,因此给我一些解决方案以在xml中添加下面的节点。

<linkbase:schemaRef 
     xlink:type="simple" 
     xlink:arcrole="http://www.w3.org/1999/xlink/properties/linkbase" 
     xlink:href="http://www.mca.gov.in/XBRL/2011/08/27/Taxonomy/CnI/ci/in-gaap-ci-2011-03-31.xsd">
</linkbase:schemaRef>

My program: 我的程序:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create the XmlDocument.
            XmlDocument XDoc = new XmlDocument();

            XmlDeclaration declaration = XDoc.CreateXmlDeclaration("1.0", "UTF-8", "");

            XmlElement XElemRoot = XDoc.CreateElement("xbrli", "xbrl", "http://www.xbrl.org/2003/instance" );
            XElemRoot.SetAttribute("xmlns:in-ca-types", "http://www.xbrl.org/in/2011-03-31/in-ca-types");
            XElemRoot.SetAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
            XElemRoot.SetAttribute("xmlns:net", "http://www.xbrl.org/2009/role/net" );
            XElemRoot.SetAttribute("xmlns:num", "http://www.xbrl.org/dtr/type/numeric" );
            XElemRoot.SetAttribute("xmlns:in-roles", "http://www.xbrl.org/in/2011-03-31/in-gaap-roles" );
            XElemRoot.SetAttribute("xmlns:link", "http://www.xbrl.org/2003/linkbase" );
            XElemRoot.SetAttribute("xmlns:negated", "http://www.xbrl.org/2009/role/negated");
            XElemRoot.SetAttribute("xmlns:iso4217", "http://www.xbrl.org/2003/iso4217" );
            XElemRoot.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
            XElemRoot.SetAttribute("xmlns:xbrldt","http://xbrl.org/2005/xbrldt"); 
            XElemRoot.SetAttribute("xmlns:in-gaap","http://www.xbrl.org/in/2011-03-31/in-gaap"); 
            XElemRoot.SetAttribute("xmlns:nonnum","http://www.xbrl.org/dtr/type/non-numeric" );
            XElemRoot.SetAttribute("xmlns:in-gaap-ci","http://www.xbrl.org/in/2011-03-31/in-gaap-ci" );
            XElemRoot.SetAttribute("xmlns:in-ca","http://www.xbrl.org/in/2011-03-31/in-ca" );
            XElemRoot.SetAttribute("xmlns:xhtml","http://www.w3.org/1999/xhtml" );
            XElemRoot.SetAttribute("xmlns:in-ca-roles", "http://www.xbrl.org/in/2011-03-31/in-ca-roles");

            XDoc.AppendChild(declaration);
            XDoc.AppendChild(comment);
            XDoc.AppendChild(XElemRoot);

            XDoc.Save(@"C:\XBRLDoc.xml");
        }
    }

You can use something like 您可以使用类似

xmlDoc.CreateElement("TesteElement", parentElem.NamespaceURI);

And this can help you, in other cases: 在其他情况下,这可以为您提供帮助:

How to prevent blank xmlns attributes in output from .NET's XmlDocument? 如何防止从.NET的XmlDocument输出的空白xmlns属性?

this will create node like this 这将创建这样的节点

<linkbase:schemaRef type="simple" arcrole="http://www.w3.org/1999/xlink/properties/linkbase" href="http://www.mca.gov.in/XBRL/2011/08/27/Taxonomy/CnI/ci/in-gaap-ci-2011-03-31.xsd" xmlns:linkbase="http://www.xbrl.org/2003/instance" />

but i want to remove that xmlns attribute. 但我想删除该xmlns属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM