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"?>
Related
I have a C# ashx handler that will do some processes and then generate an XML output which should be parsed and sent to the server.
The problem is when I try to set the context Response to txt/xml it will generate the following error:
This page contains the following errors:
error on line 1 at column 226: attributes construct error
Below is a rendering of the page up to the first error
It may look that the problem is with generated XML which may be not formatted well, but I've checked that by getting the output as a string then I used a validator and it was fine.
Any suggestions please to figure it out?
XML Output:
<Response>
<GetDigits action='http://domain/Handler.ashx?ivrlevel=9&language=arb&isOperation=false&dayFrom=3&dayTo=3&hourFrom=12&hourTo=2&minFrom=30&minTo=20&enteredMobileNumber=66355356'>
<Play>http://domain/AllClips/enterednumberis_arb.mp3</Play>
<Play>http://domain/AllClips/6.mp3</Play>
<Play>http://domain/AllClips/6.mp3</Play>
<Play>http://domain/AllClips/3.mp3</Play>
<Play>http://domain/AllClips/5.mp3</Play>
<Play>http://domain/AllClips/5.mp3</Play>
<Play>http://domain/AllClips/3.mp3</Play>
<Play>http://domain/AllClips/5.mp3</Play>
<Play>http://domain/AllClips/6.mp3</Play>
<Play>http://domain/AllClips/numberconfirmation_arb.mp3</Play>
</GetDigits>
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);
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.
EDIT: My mistake the problem is I wasn't url encoding the string, ie xdoc.Load(Server.UrlEncode("website.com") );
In C# ASP.NET I am retrieving XML from a URL. The XML is in the format like so:
<html>
<head></head>
<body>
<product>
<course>
</course>
</product>
... more data
</body>
</html>
So its really HTML or at best Not valid XML. I am getting an error when I go to load this xml which is from a server on the internet.
Is the parsing/loading failing because the XML is not valid(HTML), or is it failing because I am retrieving XML from a URL instead of local file?
How can I sucessfully parse this file? Do I need to add a XML doc type, do I use a C# HTML parser?
The error is:
// ERROR: Data at the root level is invalid. Line 1, position 1.
public string getProductXML()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://www.website.com/test.aspx?a=1&b=2"); //ERROR HERE: Data at the root level is invalid. Line 1, position 1.
// NOTE www.website.com is a different server to where this code is executing
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("group");
}
If you have access to using .Net 3.5, I would recomend using Linq2Xml. Using Linq2Xml, I would use WebClient to download the source then use XElement.Parse() to parse the html (XElement.Parse() does not require the XML start tags).
I'm trying to load a document with xml in c#
the name of xml file is variable, here is problem...
string filename="test01.xml";
XmlDocument root = new XmlDocument();
root.Load(filename);
the above code give me error: unable to connect to remote server or unable to load
but the following code works
XmlDocument root = new XmlDocument();
root.Load("test01.xml");
why is that?
You can try to specify the whole path (absolute path) to the file (not only the filename).
So instead of writing "test01.xml" you can try to write "C:\[... path to the file here]\test01.xml" and it should work as intended.
If you specify only the file name, the application will probably look for the file in the current directory (value in Environment.CurrentDirectory). I just tested this in a sample application.
It's worth mentioning that if you use FileName property from OpenFileDialog class as a case with 'using variable', it contains PATH to the file (despite its name ;)).
Does your XML contains DTD declaration with URL? Most probably parser tries to resolve it, and fails, because, say, automatic proxy does not accept its request.