2011-12-16 49 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?

相關問題