2011-12-16 68 views
1

大家好我正在创建一个应用程序来动态生成XML文件。在此我想将schemalocationXSI添加到XMLRoot我该怎么做。我想补充以下如何将Schema位置和XSI动态添加到XML文件

xmlns="http://www.irs.gov/efile" 

xsi:SchemaLocation="http://www.irs.goc/efile ReturnData941.xsd" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" 

这是我的样品已动态生成

XmlDocument doc = new XmlDocument(); 
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); 

doc.AppendChild(docNode); 

XmlNode returnData = doc.CreateElement("ReturnData"); 
XmlAttribute documnetCount = doc.CreateAttribute("documentCount"); // after this i would like to add that schema 
returnData.Attributes.Append(documnetCount); 

所以,我应该得到XML代码我XML如下

<?xml version="1.0" encoding="UTF-8"?> 
<ReturnData documentCount="" xsi:SchemaLocation="http://www.irs.goc/efile ReturnData941.xsd" xmlns="http://www.irs.gov/efile" xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" /> 

回答

3

我认为你只需要添加像

 XmlAttribute attr = doc.CreateAttribute("xsi", "schemaLocation", " "); 
     attr.Value = "http://www.irs.goc/efile ReturnData941.xsd"; 
     returnData.Attributes.Append(attr); 
+0

谢谢斯科特它的工作原理 – Dotnet 2011-12-16 08:21:40

0

一个属性,我不知道这是否接近事情的最好方法?许多验证API允许您独立指定模式位置和实例文档位置,这可能比在实例中存储模式位置更有效。

总的来说,我对xsi:schemaLocation持怀疑态度。如果你正在验证实例,那通常是因为你不信任它,如果你不信任它,为什么要信任它的xsi:schemaLocation?

相关问题