XDocument.Load losing Declaration - c#

I have a XML template file like so
<?xml version="1.0" encoding="us-ascii"?>
<AutomatedDispenseResponse>
<header shipmentNumber=""></header>
<items></items>
</AutomatedDispenseResponse>
When I use XDocument.Load, for some reason the
<?xml version="1.0" encoding="us-ascii"?>
is dropped.
How do I load the file into a XDocument and not losing the declaration at the top?

I suspect it's not really dropping the declaration on load - it's when you're writing the document out that you're missing it. Here's a sample app which works for me:
using System;
using System.Xml.Linq;
class Test
{
static void Main()
{
XDocument doc = XDocument.Load("test.xml");
Console.WriteLine(doc.Declaration);
}
}
And test.xml:
<?xml version="1.0" encoding="us-ascii" ?>
<Foo>
<Bar />
</Foo>
Output:
<?xml version="1.0" encoding="us-ascii"?>
The declaration isn't shown by XDocument.ToString(), and may be replaced when you use XDocument.Save because you may be using something like a TextWriter which already knows which encoding it's using. If you save to a stream or just to a filename, it's preserved in my experience.

It is loaded. You can see it and access parts of it using:
XDocument.Parse(myDocument).Declaration

Related

Loading multiple XDocuments, and working with its documents

I wrote several lines of code but still can't get over this:
I need to load many xml docs from web library. I don't know how many documents there are so I wonder which loop should I use while loading:
XDocument doc = XDocument.Load("http://" + i);
where -i is identifiers number.
I tried loading until i get document without meaningful content (thought it is the end, the rest are empty), but problem is that there is several Xdocs that are empty in the middle of library.
XML with content looks like
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
<GR>
<header>
<identifier>53,number of doc...used for counting</identifier>
</header>
<metadata>
<P xmlns="" xsi:schemaLocation="">
<TITLE>title</TITLE>
<CERTIFICATE NAME="different names">
</CERTIFICATE>
<YEAR>
<DATE>2012-10-18T00:00:00Z</DATE>
</YEAR>
<MINIATURE>
<COPY>
<CNAME>Copy name<CNAME>
<FORMAT>obj/max/dxf/3ds/...</FORMAT>
</COPY>
</MINIATURE>
</metadata>
</GR>
</OP>
XML without content
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
Furthermore, I need to do some counting like:
Tot.no. of doc,
No. of docs per certificate <CERTIFICATE>
No. of docs for each year <YEAR><DATE>
No of docs for each format <MINIATURE><COPY><FORMAT>
and my output should look like:
<?xml version="1.0" encoding="UTF-8" ?>
<Statistic>
<DocSum>21220</DocSum>
<Certificates>
<Certificate id=”certificateName”>17098</Certificate>
…
<Certificates>
<Years>
<Year year=”2014”>23</Year>
…
</Years>
<Miniature>
<Format post=”obj”>11723</Format>
…
</Miniature>
</Statistic>
If you could give me some help, hints or tips how to deal with it.
The posted answer by smink to the following thread should get you on the right path.
C# HttpWebRequest command to get directory listing
One of the easiest ways to get a list of the files of a web directory without knowing exactly how many there are or their filenames is by parsing the html of the directory and pulling out the tags.
You can then iterate through these tags and filter them out for the files by extensions that you need. I can provide a more in-depth example if necessary.

Remove <?xml version="1.0" encoding="UTF-8"?> from string c#

i want to know how to remove :
<?xml version="1.0" encoding="UTF-8"?>
from a string data.
I have tried this but it doesn't work
string result = data.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>", "");
(I am not working with xml , it's just a response to manipulate it without header )
Let's look at your two strings. Removing the escapes they are:
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
In other words you've managed to add an extra space. Remove that and your code will succeed.
More broadly, one wonders why you are attempting to do this. Simple text processing of XML files is liable to lead to pain and suffering. Perhaps you should consider using a parser.

C# parse shoutcast XML error

I'm trying to parse XML file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<genrelist>
<genre name="00s"></genre>
<genre name="30s"></genre>
<genre name="40s"></genre>
<genre name="50s"></genre>
</genrelist>
I am using standard System.Xml deserializer, but I get an error: In document XML (0, 0) is error (my translation to english) even before start parsing that XML is invalid. How to parse this XML?
Deserialization code:
XmlSerializer serializer = new XmlSerializer(typeof(GenreList));
XmlReader reader = XmlReader.Create("http://yp.shoutcast.com/sbin/newxml.phtml", settings);
GenreList genrelist = (GenreList)serializer.Deserialize(reader);
I have had this error when XML files from other systems have some weird char at the beginning of the file.
Make sure the file starts with below:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
Sometimes it's worth opening the file in different editors to see if you can spot the rogue char.

How to read nested XML using xDocument in Silver light?

Hi currently I have a nested XMl , having the following Structure :
<?xml version="1.0" encoding="utf-8" ?>
<Response>
<Result>
<item id="something" />
<price na="something" />
<?xml version="1.0" encoding="UTF-8" ?>
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
</Result>
<NumberReturned>10</NumberReturned>
<TotalMatches>10</TotalMatches>
</Response>
Any help on how to read this using Xdocument or XMLReader will be really helpfull.
Thanks,
Subhendu
XDocument and XmlReader are both XML parsers that expect a properly formed XML as input. What you have shown is not a XML file. So the first task would be to extract the nested XML and as this is not valid XML you cannot rely on any parser to do this job. You'll need to resort to string manipulation and or regular expressions.
My suggestion would be to fix the procedure generating this invalid XML in the first place. Another suggestion is to never generate a XML file manually but use an appropriate tool for this (XmlWriter, XDocument, ...)

Loading xml data into ndbunit

I am having problem loading the testdata.xml into ndbunit,
I followed http://code.google.com/p/ndbunit/wiki/QuickStartGuide,
but the test data is not loaded when I run my unit test in NUnit.
Is there any gotcha that I am not aware of ?
I had the same problem. Then I noticed that my xml file did not have the namespace in it
i.e.
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
instead of
<?xml version="1.0" standalone="yes"?>
<NewDataSet
xmlns="http://tempuri.org/Database.xsd" >
Once I put the namespace in, my tests were happy again.

Categories