Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Help with C# and XML Transformations

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,
Matthew
0
mglace390
Asked:
mglace390
  • 2
1 Solution
 
Chester_M_RagelCommented:
If you are using the sample code from

http://msdn.microsoft.com/XML/BuildingXML/XMLinNETFramework/default.aspx?pull=/library/en-us/dnxmlnet/html/xmlcsvreader.asp

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...
0
 
mglace390Author Commented:
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?
0
 
mglace390Author Commented:
Nevermind I figured it out.

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

Thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now