locked
How to create XML using XMLWriter in c# RRS feed

  • Question

  • User469218785 posted

    Hello All,

    I am trying to write the code to generate the XML using XMLWriter.

    <sdtc:raceCode value="UNK" />

    I want to write the above code without including any extra namespace so anyone known about it then please help me.

    Thanks

    Pankaj

    Friday, March 17, 2017 1:40 PM

All replies

  • User475983607 posted

    Hello All,

    I am trying to write the code to generate the XML using XMLWriter.

    <sdtc:raceCode value="UNK" />

    I want to write the above code without including any extra namespace so anyone known about it then please help me.

    Thanks

    Pankaj

    Your question is not clear. The posted code has a namespace what exactly do you want to accomplish?  Remove the namespace?  At a high level, what are you trying to do?

    Post the code you have written so far.  Include what you expect the code to do and what actually happens.

    Other than that, consider taking a moment to reading about XML serialization in .NET and how to control serialization in using attributes. 

    https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx

    Also the XmlWrite MSDN documentation has a lot of information on how to write XML with or without namespaces.

    https://msdn.microsoft.com/en-us/library/system.xml.xmlwriter(v=vs.110).aspx

    Friday, March 17, 2017 1:55 PM
  • User469218785 posted

    Hello mgebhard,

    Thanks for replying!

    I want to write the given below XML using XMLWriter class.

    <sdtc:raceCode value="UNK" />

    please help us to write above xml, but there should not add any extra attribute to it on runtime.

    Thanks

    pankaj

    Monday, March 20, 2017 6:21 AM
  • User1771544211 posted

    Hi ShivPankaj,

    You can try the following code:

    using (XmlWriter writer = XmlWriter.Create(@"D:\\test.xml"))
    {
        writer.WriteStartElement("sdtc","raceCode","namespace");
        writer.WriteAttributeString("value", "UNK");
        writer.WriteEndElement();
        writer.Flush();
    }

    Best Regards,

    Jean

    Monday, March 20, 2017 8:54 AM
  • User469218785 posted

    Thanks for reply,

    I don't want to use any namespace because it create another attribute in same tag like  xmlns:sdtc="namespace".

    please give any specific solution for the same,

    Thanks

    Thursday, April 6, 2017 4:59 AM
  • User1771544211 posted

    Hi  ShivPankaj,

    I don't want to use any namespace because it create another attribute in same tag like  xmlns:sdtc="namespace".

    You can try the following code, it will add the namespace to the root element, all the child elements of the root element will not have the namespace.

                using (XmlWriter writer = XmlWriter.Create(@"D:\\test.xml",settings))
                {
                    const string Ns = "namespace";
                    const string Prefix = "sdtc";
    
                    //writer.S .Settings = settings;
                    writer.WriteStartElement("root");
                    writer.WriteAttributeString("xmlns", Prefix, null, Ns);
                    writer.WriteStartElement(Prefix, "raceCode",Ns);
                    writer.WriteAttributeString("value", "UNK");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.Flush();
                }

    The result:

    <root xmlns:sdtc="namespace">
      <sdtc:raceCode value="UNK" />
    </root>

    Best Regards,

    Jean

    Thursday, April 6, 2017 7:25 AM
  • User469218785 posted

    Hi Jean,

    Thank you for looking into this, But I dont want to have this scenario that you shared here, let me explain the issue.

    I want to write the below given line of xml.

    <sdtc:raceCode code="2108-9" displayName="EUROPEAN" codeSystem="2.16.840.1.113883.6.238" codeSystemName="CDC Race and Ethnicity" />

    and I am using the syntax to write above line using xmlwriter. and if i don't pass the instance namespace or pass null its throwing error.

     writer.WriteStartElement("sdtc", "raceCode", System.Xml.Schema.XmlSchema.InstanceNamespace);

    But it added the extra namespace while generaed the xml. created xml as given below

     <sdtc:raceCode code="2108-9" displayName="EUROPEAN" codeSystem="2.16.840.1.113883.6.238" codeSystemName="CDC Race and Ethnicity" xmlns:sdtc="http://www.w3.org/2001/XMLSchema-instance" />

    so I dont want to have any specific namepasce either in root element or its child node, so please suggest me for the exact solution.

    Thanks in advance!

    Tuesday, April 11, 2017 4:59 AM
  • User1771544211 posted

    Hi ShivPankaj,

    so I dont want to have any specific namepasce either in root element or its child node, so please suggest me for the exact solution.

    I tested various methods with XmlWriter, but one of them can have the prefix and don't have the namespace at the same time.

    I try using the TextWriter to create the XML file and write the content into the file as you expected. Please take the following code as reference:

    using (TextWriter writer = File.CreateText("D:\\test2.xml"))
    {
        //
        // Write one line.
        //
        writer.WriteLine("<sdtc:raceCode value=\"UNK\" />");
    
    }

    Best Regards,

    Jean

    Tuesday, April 18, 2017 5:59 AM