locked
Uploading XML data file with encoding issue RRS feed

  • Question

  • User-34860367 posted

    Hello all,

    I want to upload XML data into SQL table but I got an error: XML parsing: line 1, character 64, unable to switch the encoding

    <?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
    <TestData>
    	<xsd:schema id="TestData" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    		<xsd:element name="TestData" msdata:IsDataSet="true">
    			<xsd:complexType>
    				<xsd:choice maxOccurs="unbounded">
    					<xsd:element name="master_mcl" minOccurs="0" maxOccurs="unbounded">
    						<xsd:complexType>
    							<xsd:sequence>
    								<xsd:element name="orderno">
    									<xsd:simpleType>
    										<xsd:restriction base="xsd:string">
    											<xsd:maxLength value="6"/>
    										</xsd:restriction>
    									</xsd:simpleType>
    								</xsd:element>
    								<xsd:element name="item">
    									<xsd:simpleType>
    										<xsd:restriction base="xsd:string">
    											<xsd:maxLength value="35"/>
    										</xsd:restriction>
    									</xsd:simpleType>
    								</xsd:element>
    								<xsd:element name="price">
    									<xsd:simpleType>
    										<xsd:restriction base="xsd:string">
    											<xsd:maxLength value="10"/>
    										</xsd:restriction>
    									</xsd:simpleType>
    								</xsd:element>
    							</xsd:sequence>
    						</xsd:complexType>
    					</xsd:element>
    				</xsd:choice>
    				<xsd:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" processContents="lax"/>
    			</xsd:complexType>
    		</xsd:element>
    	</xsd:schema>
            <Invoice>
    		<orderno>1234</orderno>
    		<item>book</item>
    		<price>$50.00</price>
    	</Invoice>
            <Invoice>
    		<orderno>5678</orderno>
    		<item>food</item>
    		<price>$25.00</price>
    	</Invoice>
    </TestData>

    How do I replace <?xml version = "1.0" encoding="windows-1252" standalone="yes"?> with <?xml version = "1.0" encoding="UTF-16" standalone="yes"?> and delete all <xsd> section in above sample.xml file?

    Thanks in advance.

    Monday, September 26, 2016 3:32 PM

Answers

  • User-34860367 posted

    Finally, I found easy way to solve my issue. Just use C# Replace String.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 26, 2016 6:08 PM

All replies

  • User1070236303 posted

    If you have an XmlDocument, you can remove the encoding this way:

    if( doc.FirstChild is XmlDeclaration )
    {
        ( ( XmlDeclaration )doc.FirstChild ).Encoding = "";
    }

    If you have plain text, my guess you can figure out a simple text replace by own.

    Removing the xsd programatically:

    XmlNamespaceManager manager = new XmlNamespaceManager( doc.NameTable );
    manager.AddNamespace( "xsd", "http://www.w3.org/2001/XMLSchema" );
    
    foreach( XmlNode node in doc.SelectNodes( "//xsd:schema", manager ) )
    {
        node.ParentNode.RemoveChild( node );
    }
    

    Monday, September 26, 2016 3:45 PM
  • User-34860367 posted

    Thanks. I am not sure how to apply your code into my method using web service as following:

     protected void UploadXML(object sender, EventArgs e)
            {
                string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string filePath = Server.MapPath("~/Repository/") + fileName;
                FileUpload1.SaveAs(filePath);
                string xml = File.ReadAllText(filePath);

    // ??? need to manipulate xml string read from above sample.xml after change encoding and get rid off xsd schema section ServiceReference.UploadDataServiceClient ws = new XMLServiceReference.UploadDataServiceClient(); ws.XMLUpload(xml); }

    Any further help is much appreciated.

    Monday, September 26, 2016 4:05 PM
  • User-34860367 posted

    Finally, I found easy way to solve my issue. Just use C# Replace String.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 26, 2016 6:08 PM