I have a really simple XML file that I'm trying to read, but I can't seem to get it working. Here is the XML file:
<?xml version="1.0"?> <Results><One>45364634</One><Two>-1</Two><Three>B</Three></Results>
I am trying to get the contents of two like this:
XmlNode node = doc.DocumentElement.SelectSingleNode("/Results/Two");
or
XmlNodeList list = doc.GetElementsByTagName("Two");
Neither is working. When I copy paste the XML as a string into the XmlDocument, then it works. However, when I use the string I pull out of the response (where I'm getting the XML from), it doesn't work.
I'm wondering if it's something weird like a character issue or not looking at the correct root, but I can't figure it out. Any ideas?
Thanks!
Check the Xml file encoding ...
Is it ansi? utf-8 or utf-16?
Check if the xml was loaded from the file at all. Check if there is any error, see if the document was populated.
I think the document is not being populated when loading from the file.
By your use of the word "response" I am assuming you are passing the xml via http? If so, try using HttpServerUtility.HtmlDecode( xml ) see if that works
Bleh.
Turns out I was returning an XML document within an XML document. That's why printing to the screen looked ok but I couldn't pull it out.
Thanks guys.
Related
I am working with some xml in C# and am having some issues parsing an xml file due to the format it is in. It has non xml data in the file and I have no control over the format of this file. The file is "test.xml"(see below). I am only concerned with the xml portion of the data, but am unsure the best way to go about accessing it. Any thoughts or recommendations would be greatly appreciated.
Test data -1
Smith, 2234
##*j
Random--
#<?xml version="1.0" encoding="utf-16"?>
<ConfigMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.Test.com/schemas/Test.test.Config">
<Config>
<Version>10</Version>
<Build>00520</Build>
<EnableV>false</EnableV>
<BuildL>22</BuildL>
<BuildP>\\testpath\test</BuildP>
</Config>
</ConfigMessage>
#
Put the whole file into a string that contains anything within the first '<' and the last '>' characters detected on the file. Then you can treat it as normal XML from there. If there's random non-XML elements throughout it though you will need to add additional logic to detect starting/stopping XML "blocks".
I can suggest you such solution: open your pseudo-xml like simple text-file, read whole text, after that, with using regex you ought to take xml document (part of primordial document that is able to be converted to XML [|startTag|any symbols|/endTag|]), put it into XDocument (in memory) and now parse it like XML-file.
I have a problem for loading xml in c #.
XmlDocument doc = new XmlDocument();
string xmlText = File.ReadAllText("D:\\webservice_aspnet\\novo2.xml");
doc.PreserveWhitespace = true;
doc.LoadXml(xmlText);
Above do loading the file.
Original file:
<?xml version="1.0" encoding="UTF-8"?>
<teste>
<abc xmlns="xxx"/>
</teste>
When I try doc.InnerXml, and create a xml file, it looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<teste>
<abc xmlns="xxx" />
</teste>
See that a space was added here:
<abc xmlns="xxx" />
at the end of the tag. I know this does not alter the structure of the file, however I have a validation algorithm that file and I can not change anything or add a space.
I do not want to replace to fix this, because they are giants and files can lose information.
Anyone know how I can generate the identical file?
If you are reading XML using a parser (perhaps a home-brew parser) than can't handle all legal XML syntax, then you are storing up trouble, and your name will be cursed by anyone who inherits your code. Don't do it.
Don't try to fix the XML generation code to generate the subset of XML that your parser can handle. Fix your parser.
One way to fix your parser might be to add an XML canonicalization step as the first thing it does; canonicalization generates a well defined subset of XML that might (if you're lucky) correspond to the subset that your home-brew parser understands.
Perhaps you could try doc.Load(file) instead of doc.LoadXml(file).
Please try just using Replace function:
string YourXML=SomexmlContent;
string result=YourXML.Replace(" />","/>");
Hope this helps!
I'm trying to read my Apple Safari history with c#, which is stored in a plist file, however I always get an error and I'm not sure what the correct way is to do it.
The code I tried to execute is this:
XmlDocument xmd = new XmlDocument();
xmd.LoadXml(#"C:\Users\Oran\AppData\Roaming\AppleComputer\Safari\History.plist");
and I always get the following error:
"Data at the root level is invalid. Line 1, position 1."
Does anyone know whats wrong with this code and recommend what is the best way to read plist files?
It looks like that Apple Safari history.plist is binary plist. I've found a great project:
https://github.com/animetrics/PlistCS
From the readme:
This is a C# Property List (plist) serialization library (MIT
license). It supports both XML and binary versions of the plist
format.
try this and everyhing should be fine ;-)
xmd.Load(...)
The one you have used loads the xml data from a string not from a file.
A plist doesn't have to be XML. There are four different serialization methods — old-style (for NeXT; no longer used), XML, binary and JSON (new in 10.7). Safari's History.plist is most likely binary, for efficiency reasons.
If I'm not mistaken, Safari for Windows does ship with plutil.exe in Common Files\Apple Application Support. You can use that like plutil -convert xml1 SOME_FILE.plist to convert your file.
The problem is with the second line, saying
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Option 1. Remove it before parsing.
Option 2. Read the MSDN on
"XmlDocument.XmlResolver Property" and figure out how to make the
XmlDocument download, parse and use the DTD from the URI specified in the XML.
I'm using XSLT transfer an XML to a different format XML. If there is empty data with the element, it will display as a self-closing, eg. <data />, but I want output it with the closing tag like this <data></data>.
If I change the output method from "xml" to "html" then I can get the <data></data>, but I will lose the <?xml version="1.0" encoding="UTF-8"?> on the top of the document. Is this the correct way of doing this?
Many thanks.
Daoming
If you want this because you think that self closing tags are ugly, then get over it.
If you want to pass the output to some non-conformant XML Parser that is under control, then use a better parser, or fix the one you are using.
If it is out of your control, and you must send it to an inadequate XML Parser, then do you really need the prolog? If not, then html output method is fine.
If you do need the XML prolog, then you could use the html output method, and prepend the prolog after transformation, but before sending it to the deficient parser.
Alternatively, you could output it as XML with self-closing tags, and preprocess before sending it to your deficient parser with some kind of custom serialisation, using the DOM. If it can't handle self-closing tags, then I'm sure that isn't the only way in which it fails to parse XML. You might need to do something about namespaces, for example.
You could try adding an empty text node to any empty elements that you are outputting. That might do the trick.
Self-closed and explicitly closed elements are exactly the same thing in any regard whatsoever.
Only if somewhere along your processing chain there is a tool that is not XML aware (code that does XML processing with regex, for example), it might make a difference. At which point you should think about changing that part of the processing, instead of the XML generation/serialization part.
I have an InfoPath form in a SharePoint workflow. I'm trying to use a blank copy of the XML produced by the InfoPath to create new instances of the form for the document library to start the workflow, thats not where my problem is. I have an app which copies the file to the document library but when i try to populate the XML i get this error:
Data at the root level is invalid.
Line 1, position 1
at the line which reads
doc.LoadXml("copiedFile.xml");
I have no idea why it does the, as to my knowledge the XML is well formed (as this is done automatically by InfoPath) so i can't see where the problem is.
the first four lines of the XML are as follows:
<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:InfoPathForm:-myXSD-2009-10-12T13-20-27" solutionVersion="1.1.0.84" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://seed-dev1/FormServerTemplates/InfoPathForm%5B3%5D.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-10-12T13:20:27" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
Could try giving...
doc.Load("copiedFile.xml");
a go.
Your XML document header seems ok, but I can bet on encoding-related problems.
Can you please post your code to create that XML file? Have you noted a "strange" first character in your file content?
I have had this sort of problem before. I am not sure what caused it, propbably the encoding.
Open the file in a program like notepad2. Whatever you use, You need to see the whitespace. The first couple of charaters will be gibberish. delete the whitespace and then save the document.
then give your app ago.
Hopefully it will work for you.