首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用WriteElementString?

如何使用WriteElementString?
EN

Stack Overflow用户
提问于 2013-02-03 06:44:23
回答 1查看 245关注 0票数 1

我是使用C#处理XML文档的新手。

我的C#代码:

代码语言:javascript
复制
XmlNode root = xmlDoc.DocumentElement;
XmlElement childNode = xmlDoc.CreateElement("link:schemaRef");
root.AppendChild(childNode);

XmlAttribute type = xmlDoc.CreateAttribute("xlink:type");
type.Value = "simple";
childNode.Attributes.Append(type);

XmlAttribute type2 = xmlDoc.CreateAttribute("xlink:href");
type2.Value = "http://taxonomi.xbrl.se/se/fr/sme/rbf/2008-09-30/se-sme-rbf-2008-09-30.xsd";
childNode.Attributes.Append(type2);

但是,使用该代码将生成这样的XML:

代码语言:javascript
复制
<schemaRef type="simple" href="http://taxonomi.xbrl.se/se/fr/sme/rbf/2008-09-30/se-sme-rbf-2008-09-30.xsd" />

但是,我希望生成的XML元素如下所示:

代码语言:javascript
复制
<link:schemaRef xlink:type="simple" xlink:href="http://taxonomi.xbrl.se/se/fr/sme/rbf/2008-09-30/se-sme-rbf-2008-09-30.xsd" />

我想我已经找到了我的问题的解决方案,下面是我的C#代码,作为我的临时解决方案:

代码语言:javascript
复制
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"" + dir + "" + filename);

XmlNode root = xmlDoc.DocumentElement;

//##Insert XBRL link scheme
var schemaRefElement = xmlDoc.CreateElement("link", "schemaRef", "urn:linkbase");
schemaRefElement.SetAttribute("type", "http://www.w3.org/1999/xlink", "simple");
schemaRefElement.SetAttribute("href", "http://www.w3.org/1999/xlink","http://www.bi.go.id/xbrl/2012-06 18/view/Pelaporan%20Keuangan/Rincian%20aset%20non%20finansial/" + reportname + "/" + reportname + ".xsd");
root.AppendChild(schemaRefElement);
xmlDoc.Save(@"" + dir + "" + filename);

但我仍然在寻找其他最好的解决方案;实际上,我更喜欢使用LINQ。

EN

回答 1

Stack Overflow用户

发布于 2014-03-06 14:59:24

试试这个;

代码语言:javascript
复制
XmlDocument doc = new XmlDocument();
var mmd = doc.CreateElement("mmd");
var element = doc.CreateElement("link", "schemaRef", "http://www.xbrl.org/2003/linkbase");
            mmd.AppendChild(element);

XmlAttribute xmlAttribute = doc.CreateAttribute("xlink", "type", "http://www.w3.org/1999/xlink");
xmlAttribute.Value = "simple";
element.Attributes.Append(xmlAttribute);

xmlAttribute = doc.CreateAttribute("xlink", "href", "http://www.w3.org/1999/xlink");
xmlAttribute.Value = "http://taxonomi.xbrl.se/se/fr/sme/rbf/2008-09-30/se-sme-rbf-2008-09-30.xsd";
element.Attributes.Append(xmlAttribute);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14670122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档