none
Add XML namespace RRS feed

  • Question

  • I have the code below to write a simple collada dae for a project I am working on. The code works but not in the way i need it to.

    It outputs this: <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">

                           <stuffinside xmlns=" "></stuffinside>

                           </COLLADA>

    Instead of this: <COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">

                           <stuffinside></stuffinside>

                           </COLLADA>

    Here is my code:

    XNamespace xNamespace = "http://www.collada.org/2005/11/COLLADASchema";
                XDocument doc =
                    
                new XDocument(
                    
                new XElement(xNamespace + "COLLADA",new XAttribute("xmlns", "http://www.collada.org/2005/11/COLLADASchema"),new XAttribute("version", "1.4.1"),
                      new XElement("asset", new XElement("contributor", new XElement("authoring_tool", "sc3dparser")), new XElement("created", DateTime.Now),
                      new XElement("unit", new XAttribute("name", "meter"), new XAttribute("meter", "1")), new XElement("up_axis", "Y_UP")),
                      new XElement("library_geometries", new XElement("geometry", new XAttribute("id", "cube-mesh"), new XAttribute("name", "Cube"), new XElement("mesh",
                      new XElement("source", new XAttribute("id", "cube-mesh-positions"), new XElement("float_array", new XAttribute("id", "cube-mesh-positions-array"),
                      new XAttribute("count", "24"), "-1 -1 -1 -1 -1 1 -1 1 -1 -1 1 1 1 -1 -1 1 -1 1 1 1 -1 1 1 1"
                      ), new XElement("technique_common", new XElement("accessor", new XAttribute("source", "#cube-mesh-positions-array"), new XAttribute("count", "8"), new XAttribute("stride", "3"),
                     list.Select(x => new XElement("param", new XAttribute("name", x), new XAttribute("type", "float"))
                    )))
    
    
                      ), new XElement("source", new XAttribute("id", "cube-mesh-normals"), new XElement("float_array", new XAttribute("id", "cube-mesh-normals-array")
                      , new XAttribute("count", "18"), "-1 0 0 0 1 0 1 0 0 0 -1 0 0 0 -1 0 0 1"), new XElement("technique_common", new XElement("accessor", new XAttribute(
                          "source", "#cube-mesh-normals-array"), new XAttribute("count", "6"), new XAttribute("stride", "3"), list.Select(x => new XElement("param", new XAttribute("name", x), new XAttribute("type", "float")))))
    
                      ),new XElement("source",new XAttribute("id","cube-mesh-map-0"),new XElement("float_array",new XAttribute("id","cube-mesh-map-0-array"),new XAttribute("count","72"), "0.625 0 0.375 0.25 0.375 0 0.625 0.25 0.375 0.5 0.375 0.25 0.625 0.5 0.375 0.75 0.375 0.5 0.625 0.75 0.375 1 0.375 0.75 0.375 0.5 0.125 0.75 0.125 0.5 0.875 0.5 0.625 0.75 0.625 0.5 0.625 0 0.625 0.25 0.375 0.25 0.625 0.25 0.625 0.5 0.375 0.5 0.625 0.5 0.625 0.75 0.375 0.75 0.625 0.75 0.625 1 0.375 1 0.375 0.5 0.375 0.75 0.125 0.75 0.875 0.5 0.875 0.75 0.625 0.75")
                      ,new XElement("technique_common",new XElement("accessor",new XAttribute("source","#cube-mesh-map-0-array"),new XAttribute("count","36")
                      ,new XAttribute("stride","2"), list2.Select(x => new XElement("param", new XAttribute("name", x), new XAttribute("type", "float"))))
                      )),new XElement("vertices",new XAttribute("id","cube-mesh-vertices"),new XElement("input", new XAttribute("semantic","POSITION")
                      ,new XAttribute("source","#cube-mesh-positions"))),new XElement("triangles",new XAttribute("count","12"), new XElement("input",new XAttribute
                      ("semantic","VERTEX"), new XAttribute
                      ("source", "#cube-mesh-vertices"), new XAttribute
                      ("offset", "0")), new XElement("input", new XAttribute
                      ("semantic", "NORMAL"), new XAttribute
                      ("source", "#cube-mesh-normals"), new XAttribute
                      ("offset", "1")), new XElement("input", new XAttribute
                      ("semantic", "TEXCOORD"), new XAttribute
                      ("source", "#cube-mesh-map-0"), new XAttribute
                      ("offset", "2")), new XElement("p", "1 0 0 2 0 1 0 0 2 3 1 3 6 1 4 2 1 5 7 2 6 4 2 7 6 2 8 5 3 9 0 3 10 4 3 11 6 4 12 0 4 13 2 4 14 3 5 15 5 5 16 7 5 17 1 0 18 3 0 19 2 0 20 3 1 21 7 1 22 6 1 23 7 2 24 5 2 25 4 2 26 5 3 27 1 3 28 0 3 29 6 4 30 4 4 31 0 4 32 3 5 33 1 5 34 5 5 35")
                    )))
    
    
                    ),new XElement("library_visual_scenes",new XElement("visual_scene",new XAttribute("id","scene"),new XAttribute("name","Scene"),new XElement
                    ("node",new XAttribute("id","cube"),new XAttribute("name","Cube"),new XAttribute("type","NODE"),new XElement("matrix",new XAttribute("sid","transform")
                    , "1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1"),new XElement("instance_geometry",new XAttribute("url","#cube-mesh"),new XAttribute("name","Cube"))))), new XElement("scene", new XElement("instance_visual_scene", new XAttribute("url", "#scene")))
                  ));
    
                doc.Save(@"C:\Users\scion\Desktop\test.dae");


    Griffin

    Saturday, October 12, 2019 5:42 PM

All replies

  • Try something like this:

       XNamespace ns = "http://www.collada.org/2005/11/COLLADASchema";

       XDocument doc =

          new XDocument(

             new XElement( ns + "COLLADA", . . .

                new XElement( ns + "asset",  . . .

                   new XElement( ns + "contributor",  . . .

                      new XElement( ns+ "authoring_tool" . . .

    Specify the namespace for all of the elements.


    • Edited by Viorel_MVP Saturday, October 12, 2019 7:12 PM
    Saturday, October 12, 2019 7:10 PM
  • I can't do that. That isn't the output I am looking for

    Griffin

    Saturday, October 12, 2019 9:53 PM
  • Hi Garlfin,

    Thank you for posting here.

    According to your description, you don't want to generate xmlns attribute.

    You can remove them after you generate the file.

    Code:

     foreach (var node in doc.Root.Descendants().Where(n => n.Name.NamespaceName == ""))
                {
                    node.Attributes("xmlns").Remove();
                    node.Name = node.Parent.Name.Namespace + node.Name.LocalName;
                }
                doc.Save(@"D:\test.dae");

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 14, 2019 2:23 AM
    Moderator