2013-10-15 17 views
0

正試圖通過代碼生成這樣的XML文檔。在asp.net中創建XMLDocument throguh代碼

<TestRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://localhost:2292/RMSchema.xsd"> 
    <Version>3</Version> 
    <ApplicationHeader> 
     <AppLanguage /> 
     <UserId>rmservice</UserId> 
    </ApplicationHeader> 
    <CustomerData> 
     <ExistingCustomerData> 
      <MTN>2084127182</MTN> 
     </ExistingCustomerData> 
    </CustomerData> 
</TestRequest> 

我試了一些樣品。但他們爲孩子們創建了xmlns,我不需要這些xmlns。任何幫助真的很感激。

我試過下面的代碼。但它僅增加的xmlns向所有兒童,這我不需要

XmlDocument xDocument = new XmlDocument(); 
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null)); 
XmlElement xRoot = xDocument.CreateElement("TestRequest", "XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:2292/RMSchema.xsd"); 
xDocument.AppendChild(xRoot); 
xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = 1; 

感謝 楚楚

我試圖與

var xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
      XmlElement xRoot = xDocument.CreateElement("xsi","RMRequest",xsi); 
      xRoot.SetAttribute("noNamespaceSchemaLocation", xsi, "http://localhost:2292/RMSchema.xsd"); 

      xDocument.AppendChild(xRoot); 
Now the response is 

<?xml version=\"1.0\" encoding=\"windows-1252\"?><xsi:TestRequest xsi:noNamespaceSchemaLocation=\"http://localhost:2292/RMSchema.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> 
+2

您是否確實需要使用XmlDocument? LINQ to XML通常更簡單。此外,你應該顯示你已經嘗試過,所以我們可以嘗試幫助你解決它。 –

回答

3

以下是令人敬畏的LINQ to XML。請享用!

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
XDocument doc = new XDocument(new XDeclaration("1.0", "windows-1252", null), 
    new XElement("TestRequest", 
     new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), 
     new XAttribute(xsi + "noNamespaceSchemaLocation", "http://localhost:2292/RMSchema.xsd"), 
     new XElement("Version", 
       new XText("3") 
     ), 
     new XElement("ApplicationHeader", 
       new XElement("AppLanguage"), 
       new XElement("UserId", 
         new XText("rmservice") 
       ) 
     ), 
     new XElement("CustomerData", 
      new XElement("ExistingCustomerData", 
       new XElement("MTN", 
        new XText("2084127182") 
       ) 
      ) 
     ) 
    ) 
); 

doc.Save(filePath); 

如果你真的想舊的API,那就是:

var xDocument = new XmlDocument(); 
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null)); 

var xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
var xRoot = xDocument.CreateElement("TestRequest"); 

var attr = xDocument.CreateAttribute("xsi", "noNamespaceSchemaLocation", xsi); 
attr.Value = "http://localhost:2292/RMSchema.xsd"; 
xRoot.Attributes.Append(attr); 

xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = "1"; 

// .. your other elemets ... 

xDocument.AppendChild(xRoot); 
xDocument.Save(filePath); 

編輯:從您的意見,它看起來像你想在某個特定的順序xmlns:xsi和其他屬性。如果是這樣,您可能必須先欺騙XmlDocument以添加xmlns:xsi屬性。

var xDocument = new XmlDocument(); 
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null)); 

var xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
var xRoot = xDocument.CreateElement("TestRequest"); 

// add namespace decl are attribute 
var attr = xDocument.CreateAttribute("xmlns:xsi"); 
attr.Value = xsi; 
xRoot.Attributes.Append(attr); 

// no need to specify prefix, XmlDocument will figure it now 
attr = xDocument.CreateAttribute("noNamespaceSchemaLocation", xsi); 
attr.Value = "http://localhost:2292/RMSchema.xsd"; 
xRoot.Attributes.Append(attr); 

xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = "1"; 

// .. your other elemets ... 

xDocument.AppendChild(xRoot); 
xDocument.Save(filePath); 
+0

現在獲得<?xml version = \「1.0 \」encoding = \「windows-1252 \」?> Bheema

+0

var xsi =「http://www.w3.org/2001/XMLSchema-instance」; XmlElement xRoot = xDocument.CreateElement(「xsi」,「RMRequest」,xsi); xRoot.SetAttribute(「noNamespaceSchemaLocation」,xsi,「http:// localhost:2292/RMSchema.xsd」); xDocument.AppendChild(xRoot);現在m – Bheema

+0

我已經編輯了一段時間的回答,以消除將「xsi」添加到根元素的bug。刷新頁面並查看我的答案。 – YK1

0

您正在尋找這樣的事情: -

XmlDocument xmldoc = new XmlDocument(); 
XmlNode root = xmldoc.AppendChild(xmldoc.CreateElement("Root")); 
XmlNode child = root.AppendChild(xmldoc.CreateElement("Child")); 
XmlAttribute childAtt =child.Attributes.Append(xmldoc.CreateAttribute("Attribute")); 
childAtt.InnerText = "My innertext"; 
child.InnerText = "My node Innertext"; 
xmldoc.Save("ABC.xml");