2011-05-12 58 views
2
<?xml version="1.0" encoding="UTF-8"?> 
<manifest identifier="D2L_1000000179" xmlns:d2l_2p0="http://desire2learn.com/xsd/d2lcp_v2p0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> 
    <resources> 
    <resource identifier="res_question_library" type="webcontent" d2l_2p0:material_type="d2lquestionlibrary" d2l_2p0:link_target="" href="questiondb.xml" title="Question Library" /> 
    <resource identifier="res_quiz_1000000179" type="webcontent" d2l_2p0:material_type="d2lquiz" d2l_2p0:link_target="" href="quiz_d2l_1000000179.xml" title="Quiz to test D2L import" /> 
    </resources> 
</manifest> 

我想創建這個xml文件。 但是當我創建這個使用Xelement or XmlElemenmt我得到這個用命名空間創建一個xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<manifest identifier="D2L_1000000179" xmlns:d2l_2p0="http://desire2learn.com/xsd/d2lcp_v2p0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> 
    <resources> 
    <resource identifier="res_question_library" type="webcontent" material_type="d2lquestionlibrary" link_target="" href="questiondb.xml" title="Question Library" /> 
    <resource identifier="res_quiz_1000000179" type="webcontent" material_type="d2lquiz" link_target="" href="quiz_d2l_1000000179.xml" title="Quiz to test D2L import" /> 
    </resources> 
</manifest> 

這裏d2l_2p0:material_type快到像material_type

有人幫我使用.net框架來創建這個。此

代碼是在這裏跌破的XMLDocument

private void createMenifetData(string destPath, long listExam) 
{ 
    XmlDocument xDoc = new XmlDocument(); 
    XmlDeclaration xmlDeclaration = xDoc.CreateXmlDeclaration("1.0", "UTF-8", ""); 
    XmlElement rootnode = xDoc.CreateElement("manifest"); 
    xDoc.InsertBefore(xmlDeclaration, xDoc.DocumentElement); 
    rootnode.SetAttribute("identifier", "D2L_" + listExam.ToString()); 
    rootnode.SetAttribute("xmlns:d2l_2p0", "http://desire2learn.com/xsd/d2lcp_v2p0"); 
    rootnode.SetAttribute("xmlns", "http://www.imsglobal.org/xsd/imscp_v1p1"); 

    XmlElement resources = xDoc.CreateElement("resources"); 
    XmlElement resource = xDoc.CreateElement("resource"); 
    resource.SetAttribute("identifier", "res_question_library"); 
    resource.SetAttribute("type", "webcontent"); 
    resource.SetAttribute(@"d2l_2p0:material_type", "d2lquestionlibrary"); 
    resource.SetAttribute(@"d2l_2p0:link_target", ""); 
    resource.SetAttribute("href", "questiondb.xml"); 
    resource.SetAttribute("title", "Question Library"); 
    resources.AppendChild(resource); 

    XmlElement resource1 = xDoc.CreateElement("resource"); 
    resource1.SetAttribute("identifier", "res_quiz_" + listExam); 
    resource1.SetAttribute("type", "webcontent"); 
    resource1.SetAttribute(@"d2l_2p0:material_type", "d2lquiz"); 
    resource1.SetAttribute(@"d2l_2p0:link_target", ""); 
    resource1.SetAttribute("href", "quiz_d2l_" + listExam + ".xml"); 
    resource1.SetAttribute("title", "Quiz to test D2L import"); 
    resources.AppendChild(resource1); 

    rootnode.AppendChild(resources); 
    xDoc.AppendChild(rootnode); 
    xDoc.Save(destPath + "\\imsmanifest.xml");} 
+0

請顯示你的代碼。否則,我們無法修復它。 – 2011-05-12 12:04:14

回答

0

既然你標記這個VB我只是去簡單的路線,並使用XML文本:

Dim T = <manifest identifier="D2L_1000000179" xmlns:d2l_2p0="http://desire2learn.com/xsd/d2lcp_v2p0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> 
       <resources> 
        <resource identifier="res_question_library" type="webcontent" d2l_2p0:material_type="d2lquestionlibrary" d2l_2p0:link_target="" href="questiondb.xml" title="Question Library"/> 
        <resource identifier="res_quiz_1000000179" type="webcontent" d2l_2p0:material_type="d2lquiz" d2l_2p0:link_target="" href="quiz_d2l_1000000179.xml" title="Quiz to test D2L import"/> 
       </resources> 
      </manifest> 

這踢出:

<manifest identifier="D2L_1000000179" xmlns:d2l_2p0="http://desire2learn.com/xsd/d2lcp_v2p0" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"> 
    <resources> 
    <resource identifier="res_question_library" type="webcontent" d2l_2p0:material_type="d2lquestionlibrary" d2l_2p0:link_target="" href="questiondb.xml" title="Question Library" /> 
    <resource identifier="res_quiz_1000000179" type="webcontent" d2l_2p0:material_type="d2lquiz" d2l_2p0:link_target="" href="quiz_d2l_1000000179.xml" title="Quiz to test D2L import" /> 
    </resources> 
</manifest> 
+0

它也被標記爲'C#',也是:-) – 2011-05-12 15:46:14

+1

我從來不明白爲什麼人們將它標記爲兩者。我想我會把我的下一個問題標記爲'html'或'assembler'! – 2011-05-12 16:27:48

+0

因爲很容易從vb代碼獲得IDEA。 – Vir 2011-05-13 04:08:40

0

使用CreateAttribute方法來創建一個命名空間的屬性。稍後追加到應具有此屬性的xmlelement的屬性集合中。

public XmlAttribute CreateAttribute(
string qualifiedName, 
string namespaceURI 

1
XNamespace defaultNamespace = "http://www.imsglobal.org/xsd/imscp_v1p1"; 

    const string NAMESPACE_URI = "http://desire2learn.com/xsd/d2lcp_v2p0"; 
    const string NAMESPACE_PREFIX = "d2l_2p0"; 

    XNamespace otherNamespace = NAMESPACE_URI; 

    XElement root = new XElement(defaultNamespace + "manifest", 
         new XAttribute("identifier", "D2L_1000000179"), 
         new XAttribute(XNamespace.Xmlns + NAMESPACE_PREFIX, NAMESPACE_URI), 
         new XElement(defaultNamespace + "resources", 
          new XElement(defaultNamespace + "resource", 
           new XAttribute("identifier", "res_question_library"), 
           new XAttribute("type", "webcontent"), 
           new XAttribute(otherNamespace + "material_type", "d2lquestionlibrary"), 
           new XAttribute(otherNamespace + "link_target", ""), 
           new XAttribute("href", "questiondb.xml"), 
           new XAttribute("title", "Question Library")), 
          new XElement(defaultNamespace + "resource", 
           new XAttribute("identifier", "res_quiz_1000000179"), 
           new XAttribute("type", "webcontent"), 
           new XAttribute(otherNamespace + "material_type", "d2lquiz"), 
           new XAttribute(otherNamespace + "link_target", ""), 
           new XAttribute("href", "quiz_d2l_1000000179.xml"), 
           new XAttribute("title", "Quiz to test D2L import"))));