2013-10-15 4 views
0

Я пытаюсь создать XML-документ, подобный этому с помощью кода.Создание XMLDocument кода throguh в asp.net

<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 всем детям, которые мне не нужно

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); 

EDIT: Из ваших комментариев, это выглядит, как вы хотите 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

Теперь получаем 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' к корневому элементу. Обновите свою страницу и посмотрите мой ответ. – 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"); 
Смежные вопросы