Generate XML with multiple namespaces using XDocument

前端 未结 2 1821
礼貌的吻别
礼貌的吻别 2021-01-14 13:47

I have XML like this:



        
2条回答
  •  醉话见心
    2021-01-14 14:41

    To add namespace declaration you can use XNamespace.Xmlns, and to reference the predefined namespace prefix xml use XNamespace.Xml, for example :

    XNamespace stream = "http://etherx.jabber.org/streams";
    var result = new XElement(stream + "stream",
                        new XAttribute("from", "sourav@lap-020.abcd.co.in"),
                        new XAttribute("to","lap-020.abcd.co.in"),
                        new XAttribute(XNamespace.Xmlns + "stream", stream),
                        new XAttribute("version","1.0"),
                        new XAttribute(XNamespace.Xml+"lang","en"),
                        String.Empty);
    Console.WriteLine(result);
    //above prints :
    //
    //
    

提交回复
热议问题