Importing XML with 'µ' symbol into excel - c#

I am trying to import an XML file into excel using Data -> Other Sources -> From XML Data import. When the file contains a 'µ' symbol, it gives the following error:
Invalid file reference. The path to the file is invalid, or one or
more of the referenced schemas could not be found.
The XML looks like this:
<root>
<File>
<FileName>Data\7.5 µg_mL Sample.pdf</FileName>
</File>
</root>
If i remove the microgram symbol, it works and Excel imports the data.
I am generating the XML file in .net using XNode.toString(), and if I run the XML through a validator, it returns no errors. It doesn't seem to matter if I put the XML declaration at the top of the file and declare it as UTF-8 or 16 either.
Any pointers welcome, i would ideally like to check for any characters that might cause this problem as i am guessing there are more than just the microgram symbol.
I am passing the XML string to a function that swaps out a custom xml file, i don't seem to have the option to change the file format here..
'Uses Ionic.Zip.ZipFile
Using zip As ZipFile = ZipFile.Read(fileDest)
zip.RemoveEntry(xmlPath)
zip.Save()
zip.AddEntry(xmlPath, customXml)
zip.Save()
End Using

Per the docs for the overload of AddEntry you are using:
The content for the entry is encoded using the default text encoding for the machine
You want this to be UTF-8, so you can use the overload that allows you to specify the encoding:
zip.AddEntry(xmlPath, customXml, Encoding.UTF8);

Related

Keep special characters in File When i save it

I have a file that contains some HTML code. I am trying to load this data into a C# Console app and transfer it into a JSON file to upload somewhere. When loading the file i am losing some of the encoding immediately when bringing the data in.
Example data
<li>Comfort Range: -60°F to 30°F / -50°C to -1°C</li>
Basic read file
//Load the file
String HTML_File = File.ReadAllText(location);
//Output the file to see the text
Console.WriteLine(HTML_File);
Console Output
<li>Comfort Range: -60??F to 30?F / -50?C to -1?C</li>
After i split the data how I need to, I than save the class to a JSON File
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA));
JSON file Data
<li>Comfort Range: -60�F to 30�F / -50�C to -1�C</li>
How can i go about loading this data and converting it to JSON without losing the encoding? I am still pretty new when it comes to encoding like this.
#JeremyLakeman helped me solve this, thank you sir!! When reading the text into the utility i needed to set the Encoding but not by the default ones.
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA), Encoding.GetEncoding("iso-8859-1"));
#JeremyLakeman helped me solve this, thank you sir!! When reading the text into the utility i needed to set the Encoding but not by the default ones.
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA), Encoding.GetEncoding("iso-8859-1"));

QBO3 Attachment/Generate is called,but Attachment/Download fails

Using the QBO3 Import framework, we are generating a mail-merge document using this XML snippet:
<AttachmentItem Operation="Generate">
<Object>ImportForm</Object>
<ObjectSubscriberID>CRLetterCycle-17010353-2442587</ObjectSubscriberID>
<Template>CR CFPB RET Ack Incomplete Appl</Template>
</AttachmentItem>
and in the results, we see:
<Result ID="1816859">
<Object>Attachment</Object>
<ObjectID>524624</ObjectID>
<Operation>Insert</Operation>
<Status>Success</Status>
<Properties>
... [omitted for brevity] ...
</Properties>
</Result>
However, when we attempt to download the merged document by calling:
Attachment/Download?ID=524624
we get and HTTP 500 error, with a response that contains:
The specified file was not found in the '[...]' file object
repository. Parameter name: relativePath Actual value was
qboRoot/ImportForm/1633788/CRLetter.b6e893b7-552e-4298-9495-eaa0fb4e6a34.pdf.
Why do we get a success indicator in the import results, but cannot download the generated file?
When the QBO3 Import Framework processes the XML you provided, there are two method signatures executed:
Attachment/Insert, and
Attachment/Generate
If you inspect your import results XML a bit more, you'll find two nodes related to your attachments; the one you noted in your question, and a second one:
<Result ID="1816860">
<Object>Attachment</Object>
<ObjectID>524624</ObjectID>
<Status>Error</Status>
<Message>Invalid column name 'Item'. [Omitted for brevity]</Message>
<Source>.Net SqlClient Data Provider</Source>
</Result>
In short, the Attachment/Insert worked, creating an Attachment row in the database, but the Attachment/Generate errored, failing to save an actual mail-merged file to disk.
The error rendered in the import results is also logged in the ImportLog table, viewable (and reportable) from:
Design > Import Data > {Imported XML file}

XML Parsing Error: not well-formed when loading into browser

I am working on a project in visual studio that imports a CSV, and exports an XML file. I'd like to be able be able to get the code to work as XML and HTML, and view it in a browser. I am getting this error when I load the XML file into a browser:
Firefox
XML Parsing Error: not well-formed
Location: file:///C:/Users/fenwky/XmlDoc.xml
Line Number 2, Column 6:<?xsl:stylesheet <abc:stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">?>
Chrome
This page contains the following errors: error on line 2 at column 16: colon are forbidden from PI names 'xsl:transform'
This is what my c# code looks like in visual studio 2013:
// Create a procesing instruction.
XmlProcessingInstruction newPI;
// Stylesheet
String PItext = "<abc:stylesheet xmlns:abc=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">";
newPI = doc.CreateProcessingInstruction("abc:stylesheet", PItext);
doc.InsertAfter(newPI, doc.FirstChild);
// Save document
doc.Save(xmlfilename);
If you are trying to insert an processing instruction into the XML, the data parameter of the CreateProcessingInstruction method does not need to contain the name of the processing instruction in this case. In other words you just need to do this...
var PItext = "xmlns:abc=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"";
var newPI = doc.CreateProcessingInstruction("abc:stylesheet", PItext);
doc.InsertAfter(newPI, doc.FirstChild);
However, I am wondering why you are trying to add this particular processing instruction to an XML document. Perhaps you mean to link an XML document to a separate XSLT document, so it will be transformed if read by a browser?
If so, you probably need to be doing this...
var piText = "type=\"text/xsl\" href=\"style1.xsl\"";
var newPI = doc.CreateProcessingInstruction("xml-stylesheet", piText);
doc.InsertAfter(newPI, doc.FirstChild);
This will write the following processing instruction to the XML, which can then be read by the browser:
<?xml-stylesheet type="text/xsl" href="style1.xsl"?>

c# File in xml format not opening in xml viewer

Created a .xml file in C# console program using StreamWriter without using any xml write library functions). However, it does not show data in XML viewer - shows fine if opened as a text file.
I tried, like I saw somewhere on this site, the following -
FileStream fStream = new FileStream (#"c:\new.xml", FileMode.Create)
StreamWriter fWrite = new StreamWriter(fStream, Encoding.UTF8);
fwrite.WriteLine (myLine);
where the first myLine was
<?xml version="1.0" encoding="UTF-8"?>
Is there a way to make this open like an xml file without having to use the xml lib functions?
Here's some more info -
Contents of the file I wrote, as it opens in Notepad :
(OK, the contents are like below, but formatting isn't - the CTRL K that I was instructed to do here did the formatting!)
<?xml version="1.0" encoding="UTF-8"?>
<OutermostTag>
<RepetitiveInnerTag Action="AddSomething">
<ID1>12345<ID1>
<Level1>Leveldata1<Level1>
<DisplayName>Name to Display<DisplayName>
<Description>Describe it all here<Description>
<SortOrder>ASC<SortOrder>
<ID2>C3<ID2>
<Level2>Data<Level2>
</RepetitiveInnerTag>
</OutermostTag>
While opened as xml only the first inner tag (viz.,) data is displayed, space-demited as follows:
12345 Leveldata1 Name to Display Describe it all here ASC C3 Data
And the output display is the same whether I use the Encoding.UTF8 property or not.
By "open like an xml" I mean, in addition to displaying the entire data in the file, also make the tags collapsible (the color and all that format-related stuff that (presumably) the browser (IE) puts in)
Have you tried using flush? try putting it after fWrite.WriteLine
fWrite.Flush();
OK guys, I found out the blunder I did - didn't used the opening element tags to close them as well (OOOOPs!).Than you all for your time (and apologize to have wasted it too). a C/C++ programmer on my first C# trial project, didn't want the complications of using the XML writer libs; and now am delighted that it still works doing the lib work simply by myself the C-style (contrary to my boss's insistence that it wont:)). Will be careful next time I post

How to search through XML to find bad nodes

I have a large XML file (68Mb), I am using SQL Server Business Intelligence Studio 2008 to extract the XML data into a database. There is an error in the XML file some where that prevents it from executing. Possibly a missing tag or something like that. The file is so large I cant manually sort through it looking for the error.
Below is a sample of the the XML schema used.
How can I use XPath to sort through the XML in VS 2012 using C#?
An example would be great!
-<PhoneNumberList>
<PhoneNumber value="1234567890" type="Phone"/>
</PhoneNumberList>
-<YearsOfServiceList>
<YearsOfService experienceInMonths="24" description="SuperAdmin" objectCode="049"/>
</YearsOfServiceList>
</Person>
-<Person dob="1960-01-09T00:00:00" lastName="Smith" middleName="Will" firstName="John" id="9999-9999-9999">
-<SiteList>
-<Site id="2014" siteLongName="HA" siteCode="1255" systemCode="999">
-<StaffPositionList>
<StaffPosition id="73" staffPosition="Administrator"/>
</StaffPositionList>
</Site>
</SiteList>
-<ProgramList>
<Program id="1234" siteLongName="ABC" siteCode="0000" systemCode="205"/>
<Program id="5678" siteLongName="DEF" siteCode="0000" systemCode="357"/>
</ProgramList>
-<TypeList>
<Type Description="Leader" certificateType="D"/>
<Type Description="Professional" certificateType="P"/>
</TypeList>
-<EmailList>
<Email value="jsmith#somesite.com" type="Email"/>
</EmailList>
-<PhoneNumberList>
<PhoneNumber value="1234567890" type="Phone"/>
</PhoneNumberList>
-<YearsOfServiceList>
<YearsOfService experienceInMonths="24" description="SuperAdmin" objectCode="049"/>
</YearsOfServiceList>
</Person>
</PersonList>
</GetPersonDetail>
If you want to do it in code then create an XSD file describing a valid format for the data, embed it as a resource in your app and then use code like this
var errors = new List<string>();
var schemaSet = new XmlSchemaSet();
schemaSet.Add("", XmlReader.Create(new StringReader(Properties.Resources.NameOfXSDResource)));
document.Validate(schemaSet, (sender, args) =>
{
errors.Add(args.Message);
}
);
This will give you a list of validation errors.
You don't need to search "by hand" if you use a competent text editor. NotePad++'s XML plugin, for instance, can determine if your XML as a whole is well-formed or valid, and both instances will provide separate error messages.
If you don't have a schema and the file is well-formed, you can use the CLR's System.XML namespace to read in the document and then iterate through its nodes using LINQ-to-XML, which would allow you to very finely control which nodes go where. With LINQ, you could either create a new XML file with only the valid entries, procedurally correct the invalid entries as you determine where they are, or even just write to your SQL server database directly.
Your troubleshooting process should be something as follows:
Is the XML well-formed? I..e, does it comport to the fundamental rules of XML?
Is the XML valid? I.e., does it have the elements and attributes you expect?
Is your import query accurate?
For things like this I usually have luck checking and fixing the data in Notepad++. Install the XmlTools plugin and that has a menu for checking the xml syntax and tags.
Also, those dashes will give you problems, it's best to save out the xml file directly without copying by hand.
A 68MB XML file is no problem for XML editors such as XMLBlueprint 64-bit (http://www.xmlblueprint.com/) or Stylus Studio (http://www.stylusstudio.com/). Just check the well-formedness of your xml file (F7 in XMLBlueprint) and the editor will display the errors.

Categories