1

the output the i need is

<ns1:collection>
<ns1:child1>value1</ns1:child3>
    <ns1:child2>value2</ns1:child3>
    <ns1:child3>value3</ns1:child3>
</ns1:collection>

i try to do it by the code below - but i get an exception -...

How to add the namespace on this one ??

XDocument xDoc = new XDocument( new XElement( "collection",
                 new XElement( "ns1:child1", value1 ),
                 new XElement( "ns1:child2", value2 ),
                 new XElement( "ns1:child3", value3 ) ) );

I also try to use the

          XNamespace ns1 = "http://url/for/ns1";";

and to do

           ( ns1 + "child1", value1 )

And still nothing

Yanshof
  • 9,659
  • 21
  • 95
  • 195

2 Answers2

1

Move namespace declaration to the fictional parent element:

XDocument xDoc = new XDocument(new XElement("root",
            new XAttribute(XNamespace.Xmlns + "ns1", ns1),
                new XElement(ns1 + "collection",
                    new XElement(ns1 + "child1", value1),
                    new XElement(ns1 + "child2", value2),
                     new XElement(ns1 + "child3", value3))));
IDeveloper
  • 1,249
  • 2
  • 13
  • 22
0
        XDocument xDoc = new XDocument(new XElement(ns1+"collection",
            new XAttribute(XNamespace.Xmlns+"ns1",ns1),
             new XElement(ns1+ "child1", value1),
             new XElement(ns1+"child2", value2),
             new XElement(ns1+"child3", value3)));
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
  • But on this i will have the collection with the namespace - and i need the "collection" in as clear case => this is not good :( – Yanshof Oct 21 '13 at 04:05