none
Using XSD to create a XML file RRS feed

  • Question

  • I have tried to generate a xml file by creating xsd with the class hierarchy as needed for the xml.

    But the xml file generated contains namespace and other information as mentioned below

    

    How to get ride of the namespace in the xml file?

    I need the xml sample as mentioned below. 

    <?xml version="1.0" encoding="utf-8"?>
    <EMPLOYEES>
    <EMPLOYEE>
    <Name>xyz</Name>
    <ID>1</ID>
    <ADDRESS>
    <Door_x0020_No>15</Door_x0020_No>
    <Street>wwwww</Street>
    <City>YYYYY</City>
    <State>zzzz</State>
    </ADDRESS>
    <Salary>20000</Salary>
    <Join_x0020_Date>2018-10-16T00:00:00+05:30</Join_x0020_Date>
    </EMPLOYEE>
    </EMPLOYEES>


    Suggest any better ways to do this. I used xsd as i have large number of class in my xml file.

     
    Tuesday, October 16, 2018 8:34 AM

All replies

  • Hello, 

    How to get ride of the namespace in the xml file?

    You need to give to Serializer a Namespaces collection with single empty-empty entry. 

    -----

    The file you expecting you can generate using XSLT applied to your XSD.


    Sincerely, Highly skilled coding monkey.

    Tuesday, October 16, 2018 8:51 AM
  • Hi Manoj 1503,

    Here is the document about How to: Create an XML document based on an XSD schema for your reference.

    https://docs.microsoft.com/en-us/visualstudio/xml-tools/how-to-create-an-xml-document-based-on-an-xsd-schema?view=vs-2017

    In addition, you could also generate related XSD schema via xml file by using XSD.exe, which could use command in visual studio command prompt. 

    xsd c:\xmlpath\Test.xml /outputdir:C:\outputpapth


    https://blogs.msdn.microsoft.com/charlie/2007/12/01/from-xml-to-xsd/

    Best regards,

    Zhanglong


    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.

    Wednesday, October 17, 2018 6:20 AM
    Moderator
  • Thank you.

    >The file you expecting you can generate using XSLT applied to your XSD.

    could you share any sample for using XSLT?

    I have used to create xml with namespace as empty, still i get all the xsd declaration in my file.

    using (var stream = new FileStream(path, FileMode.Create))
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("","");

                    XmlSerializer ser = new XmlSerializer(typeof(Emp.EMPLOYEESDataTable));
                    XmlTextWriter xmlWriter = new XmlTextWriter(stream, Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    ser.Serialize(xmlWriter, dataObj, ns);
                }

    By using above code i created my xml file.

    Wednesday, October 17, 2018 8:23 AM
  • If I understand you correcty: Remove all namespace declarations in the generated classes.
    Wednesday, October 17, 2018 8:33 AM
  • This was too long ago to remember all details, but try this:

    XmlSerializerNamespaces ns = new XmlSerializerNamespaces(
                             new[] { XmlQualifiedName.Empty });

    -----

    XSLT solution - possible, but implementation will take too much of my time - I need to do my job. 


    Sincerely, Highly skilled coding monkey.

    Wednesday, October 17, 2018 10:28 AM