Help with C# and XML Transformations

Posted on 2004-11-15
Last Modified: 2013-11-19
I'm trying to convert a comma delimited text file into an XML document then apply a schema to the XML to ensure all the data coming in is of the proper format.  I've been using the XmlCsvReader class (found on MSDN) to convert the text file into the XML document.  The only problem I'm having is that the XmlCsvReader does not insert the xml declaration on the first line or  the namespace in the root element of the new XML document.  As a result, when I attempt to apply the schema, none of the elements are recognized.

How can I insert the XML declaration and the namespace into the new XML document?

Thank you,
Question by:mglace390
    LVL 6

    Accepted Solution

    If you are using the sample code from

    then add these after doc.Load(reader),

    XmlElement root = doc.DocumentElement;
    XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
    doc.InsertBefore(xmldecl, root);

    and now write the xml file...
    LVL 2

    Author Comment

    That worked wonderfully however I'm still dealing with this namespace issue.  I can't get my namespace to show up in my root element.  Any ideas?
    LVL 2

    Author Comment

    Nevermind I figured it out.

    XmlElement root = doc.DocumentElement;
    root.SetAttribute("xmlns", "urn:namespace");

    Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now