How can I write xml with a namespace and prefix with XElement?

后端 未结 5 1428
北海茫月
北海茫月 2020-12-01 23:31

This may be a beginner xml question, but how can I generate an xml document that looks like the following?



        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 00:13

    XNamespace ci = "http://somewhere.com";
    XNamespace ca = "http://somewhereelse.com";
    XElement root = new XElement(aw + "root",
        new XAttribute(XNamespace.Xmlns + "ci", "http://somewhere.com"),
        new XAttribute(XNamespace.Xmlns + "ca", "http://somewhereelse.com"),
        new XElement(ci + "field1", "test"),
        new XElement(ca + "field2", "another test")
    );
    Console.WriteLine(root);
    

    This should output

    
        test
        another test
    
    

提交回复
热议问题