none
No declaration inside xDocument RRS feed

  • Question

  • Hello,
    my target is that.

    <?xml version="1.0" encoding="iso-8859-1"?>
    <Control xsi:noNamespaceSchemaLocation="control-1.0.xsd" locale="english" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <controlRequest requesttime="2016-04-05T11:03:32+02:00" requestID="99" order="301865492">
        <properties>
          <orderProperties>
            <orderProperty name="nom
    


    I've tried serialization with xmlwriter. It's not working.
    Now with xdocument. it does not work either, what am I doing wrong?

    string UseXDocument()
    {
    	//<? xml version = "1.0" encoding = "UTF-8" ?>
    	//< pageDefinition xmlns = "http://xmlns.oracle.com/adfm/uimodel" Package = "oracle.webcenter.portalapp.pages" />
    
    	string ret = "";  
    
    	XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
    	XNamespace noName = "control-1.0.xsd";
    
    	XDocument xmlDoc = new XDocument(
    		new XDeclaration("1.0", "UTF-8", "yes"),
    		new XElement("Root",
    		new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
    		new XAttribute(XNamespace.Xmlns + "xsi" + "noName", noName.NamespaceName),
    		new XElement("controlRequest",
    			 new XAttribute("requestTime", DateTime.Now),
    			 new XAttribute("requestID", "99"),
    			 new XAttribute("order", "30186549822"),
    			new XElement("Properties",
    			  new XElement("orderProperties",
    			  new XElement("orderProperty", new XAttribute("name", "nominalValue")),
    			  new XElement("orderProperty", new XAttribute("name", "numberOfUnits")),
    			  new XElement("orderProperty", new XAttribute("name", "Index"))
    			  ), // orderProperties
    			  new XElement("materialProperties",
    			  new XElement("materialProperty", new XAttribute("name", "name1")),
    			  new XElement("materialProperty", new XAttribute("name", "name2")),
    			  new XElement("materialProperty", new XAttribute("name", "name3"))
    			  ) // materialProperties
    			  ) // Properties
    		)// ControlRequest
    		)// Root
    	);
    	// xmlDoc.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
    
    	//xmlDoc.Add(root);
    	//xmlDoc.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
    	ret = xmlDoc.ToString();
    	string s2 = xmlDoc.Declaration.ToString();      
    	return ret;
    }

    Output is

    Missing declaration
    <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsinoName="control-1.0.xsd">
      <controlRequest requestTime="2018-01-30T16:21:00.344368+01:00" requestID="99" order="30186549822">
        <Properties>
          <orderProperties>
            <orderProperty name="value1" />
            <orderProperty name="value2  " />
            <orderProperty name="value3" />
          </orderProperties>
          <materialProperties>
            <materialProperty name="name1" />
            <materialProperty name="name2" />
            <materialProperty name="name3" />
          </materialProperties>
        </Properties>
      </controlRequest>
    </Root>
    

    ok and nok

    With best regards Markus

    Tuesday, January 30, 2018 5:51 PM

Answers

  • Hi Markus Freitag,

    Please try the code below.

     static void Main(string[] args)
            {
               XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
    
                XElement root = new XElement("Control",
                    new XAttribute(xsi + "noNamespaceSchemaLocation", "control-1.0.xsd"),
                    new XAttribute("locale", "english"),
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi)
                );
                XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "ISO-8859-1", ""),
                    root);
                xDoc.Save("xDoc.xml");
    
    
                Console.ReadKey();
    
            }



    Best Regards,

    Wendy


    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.

    • Marked as answer by Markus Freitag Tuesday, February 6, 2018 5:16 PM
    Thursday, February 1, 2018 1:53 AM
    Moderator
  • Replace

    new XAttribute(XNamespace.Xmlns + "xsi" + "noName", noName.NamespaceName)

    with

    new XAttribute(xsi + "noNamespaceSchemaLocation", noName.NamespaceName)

    Hope, it helps,

    wizend

    Wednesday, January 31, 2018 5:54 PM

All replies

  • By XDocument and ToString() you won't get the desired output. First save your XDocument to a file or a MemoryStream (or, maybe, to a string by using StringWriter?) and then print it. Take a closer look at various proposals made in this stackoverflow thread for further enlightenment.

    wizend

    Wednesday, January 31, 2018 9:51 AM
  • Hello,

    <Control xsi:noNamespaceSchemaLocation="control-1.0.xsd"
     locale="english" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    You are right, works with declaration now. Thanks.

    But how can I write this Namespace, as requested by my customer?

    Can you help me, please?

    With best regards Markus

    Wednesday, January 31, 2018 4:49 PM
  • Replace

    new XAttribute(XNamespace.Xmlns + "xsi" + "noName", noName.NamespaceName)

    with

    new XAttribute(xsi + "noNamespaceSchemaLocation", noName.NamespaceName)

    Hope, it helps,

    wizend

    Wednesday, January 31, 2018 5:54 PM
  • Hi Markus Freitag,

    Please try the code below.

     static void Main(string[] args)
            {
               XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
    
                XElement root = new XElement("Control",
                    new XAttribute(xsi + "noNamespaceSchemaLocation", "control-1.0.xsd"),
                    new XAttribute("locale", "english"),
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi)
                );
                XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "ISO-8859-1", ""),
                    root);
                xDoc.Save("xDoc.xml");
    
    
                Console.ReadKey();
    
            }



    Best Regards,

    Wendy


    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.

    • Marked as answer by Markus Freitag Tuesday, February 6, 2018 5:16 PM
    Thursday, February 1, 2018 1:53 AM
    Moderator