如何在C#中添加XML文档中的命名空间
问题描述:
这是我要添加的子节点,但如果我使用createElement
方法,它会自动包含xmlns
属性,但它不是必需的,所以给我一些解决方案,以便在xml中添加下面的节点。如何在C#中添加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>
我的程序:
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");
}
}
答
您可以使用类似
xmlDoc.CreateElement("TesteElement", parentElem.NamespaceURI);
这可以帮助你,在其他情况下:
How to prevent blank xmlns attributes in output from .NET's XmlDocument?
答
这将创造no像这样
<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" />
但我想要删除该xmlns属性。
没有人回复帖子 – 2012-03-06 07:48:30