How to create a XML with multiple attributes in nodes


XML ドキュメントで名前空間を管理する | マイクロソフト ラーン[^]

名前空間を NameTable、プレフィックスを修正すると、 XmlDocument を追加します xmlns あなたのための属性。 注意: 実際に使用されている名前空間のみを追加します。この場合、Soap 名前空間のみを使用したため、 tempurischemas.microsoft.com 名前空間は省略されます。


var nt = new NameTable();
var nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("soapenv", soapNs);
nsmgr.AddNamespace("tem", "http://tempuri.org/");
nsmgr.AddNamespace("arr", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");

var doc = new XmlDocument(nt);
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
var root = doc.AppendChild(doc.CreateElement("soapenv", "Envelope", SoapNamespace));
var body = root.AppendChild(doc.CreateElement("soapenv", "Body", SoapNamespace));

切り替えることができれば XLinq[^]、名前空間の管理がはるかに簡単になります。


XNamespace SoapNs = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace TempNs = "http://tempuri.org/";
XNamespace ArrayNs = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";

var doc = new XDocument(
    new XDeclaration("1.0", "utf-8", null),
    new XElement(SoapNs + "Envelope",
        new XAttribute(XNamespace.Xmlns + "soapenv", SoapNs),
        new XAttribute(XNamespace.Xmlns + "tem", TempNs),
        new XAttribute(XNamespace.Xmlns + "arr", ArrayNs),
        new XElement(SoapNs + "Body")
    )
);



Source link

コメント

タイトルとURLをコピーしました