Мне нужно программно создать xml, который выглядит так:
<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <Profile xmlns="http://google.com"> <number>123</number> <name>bob</name> </Profile> </soap12:Body> </soap12:Envelope>
В настоящее время это выглядит так:
<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <Profile xmlns="http://google.com"> <number>123</number> <name>bob</name> </Profile> </soap12:Body> </soap12:Envelope>
Код, который у меня пока есть:
using (XmlWriter writer = XmlWriter.Create(@"C:\Users\Default\Desktop\books.xml")) { writer.WriteStartElement("soap12", "Envelope", "http://www.w3.org/2001/XMLSchema-instance"); writer.WriteStartElement("soap12", "Body", null); writer.WriteStartElement("Profile", "http://google.com"); writer.WriteElementString("number", "123"); writer.WriteElementString("name", "bob"); }
Может кто-нибудь помочь мне с этим?
Спасибо!
Использование xml linq:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" + "</soap12:Envelope>"; XDocument doc = XDocument.Parse(xml); XElement envelope = doc.Root; XNamespace soap12Ns = envelope.GetNamespaceOfPrefix("soap12"); XElement body = new XElement(soap12Ns + "Body"); envelope.Add(body); XNamespace ns = "http/://google.com"; XElement profile = new XElement(ns + "Profile"); body.Add(profile); profile.Add(new object[] { new XElement(ns + "number", 123), new XElement(ns + "name", "bob") }); } } }