帮助有关xml生成的需求 [英] Help need regarding the Generation of xml
本文介绍了帮助有关xml生成的需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友
我想像这样在我的xml文件中添加验证信息
dear friends
i want to add the validation info in my xml file like this
<?xml version="1.0" encoding="utf-8"?>
<ROOTElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="yourschema.xsd">
我该如何在我的C#代码中做到这一点?
how can i do this in my c# code
推荐答案
此应做的工作
This shoud work
using System.Xml;
namespace Dummy
{
public class Sample
{
public void GenerateXmlDocument()
{
XmlDocument xdoc = new XmlDocument();
XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
xdoc.AppendChild(dec);
XmlNode rootNode = xdoc.CreateNode(XmlNodeType.Element, "ROOTElement", "");
XmlAttribute att1 = xdoc.CreateAttribute("xmlns:xsi");
att1.Value = "http://www.w3.org/2001/XMLSchema-instance";
rootNode.Attributes.Append(att1);
XmlAttribute att2 = xdoc.CreateAttribute("xsi:noNamespaceSchemaLocation");
att2.Value = "yourschema.xsd";
rootNode.Attributes.Append(att2);
xdoc.AppendChild(rootNode);
xdoc.Save(@"c:\sample.xml");
}
}
}
-修改后的代码显示添加子元素
- Modded code to show adding a child element
using System.Xml;
namespace Dummy
{
public class Sample
{
public void GenerateXmlDocument()
{
XmlDocument xdoc = new XmlDocument();
XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
xdoc.AppendChild(dec);
XmlNode rootNode = xdoc.CreateNode(XmlNodeType.Element, "ROOTElement", "");
XmlAttribute att1 = xdoc.CreateAttribute("xmlns:xsi");
att1.Value = "http://www.w3.org/2001/XMLSchema-instance";
rootNode.Attributes.Append(att1);
XmlAttribute att2 = xdoc.CreateAttribute("xsi:noNamespaceSchemaLocation");
att2.Value = "yourschema.xsd";
rootNode.Attributes.Append(att2);
xdoc.AppendChild(rootNode);
XmlElement XElemRoot = xdoc.CreateElement("ACES");
rootNode.AppendChild(XElemRoot);
XmlElement Xsource = xdoc.CreateElement("RETURN");
Xsource.SetAttribute("ReturnType", "DLR");
Xsource.SetAttribute("ToolVer", "1.0");
XElemRoot.AppendChild(Xsource);
xdoc.Save(@"c:\sample.xml");
}
}
}
这给出了
This gives
<?xml version="1.0" encoding="utf-8"?>
<ROOTElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="yourschema.xsd">
<ACES>
<RETURN ReturnType="DLR" ToolVer="1.0" />
</ACES>
</ROOTElement>
这篇关于帮助有关xml生成的需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文