1

I have a xml file which starts like this:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" 
    xmlns:kml="http://www.opengis.net/kml/2.2" 
    xmlns:atom="http://www.w3.org/2005/Atom">

When I run my below code it it gives me error 'xsi' is an undeclared prefix.

var xDoc = XDocument.Load(kmlFileUrl);  //throws error xsi' is an undeclared prefix

XNamespace ns = "http://www.opengis.net/kml/2.2";

xDoc.Root
.Elements(ns + "Document")
.Elements(ns + "Placemark")
.GroupBy(i => (string)i)
.SelectMany(g => g.Skip(1))
.Remove();

I researched on internet and found that this occurs due to missing of the xsi namespace declaration. I tried with this code

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";                   
XDocument doc = new XDocument(                          
            new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")                          
        );

Expected result:

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" 
    xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Since I am not able to load the file how do I add the namespace to the existing file node such that I do not have to change my other code lines?

Edit

The difference is the solution there has this line

  XmlReader rd = XmlReader.Create(new StringReader(strXmlFile), xset, context);

Here the StringReader taken input as string and I am using a xml file. The xml files is too long and I do not want to write extra code to first convert it in string and than do other stuff. Also please note that I am already using the XDocument xDoc variable in code to find the elements.

I was expecting someone to advise on how can I use the XDocument and also add the namespace. Hope this makes sense.

Let me know if someone wants to help and needs more inputs on this.

user1254053
  • 755
  • 3
  • 19
  • 55
  • 1
    The party that created that xml file should fix their stuff. – rene Jan 31 '18 at 07:40
  • Agree with rene. If you're getting this error, it means that whoever generated your input is giving you "XML", not XML. They're giving you a *string* that closely resembles XML but is, in fact, not XML. Get them to stop hand-mangling strings together and use appropriate XML tooling at their end so that they generate actual XML. – Damien_The_Unbeliever Jan 31 '18 at 07:42
  • The file opens in Google Earth and shows the placemarks, its just that I am not able to load it for removing the duplicate placemark – user1254053 Jan 31 '18 at 09:17
  • Just wondering there has to be a way to append the namespace in existing file. – user1254053 Jan 31 '18 at 09:19
  • https://stackoverflow.com/questions/20638058/xsi-is-an-undeclared-prefix-using-xmldocument – Jan-Peter Vos Jan 31 '18 at 10:29
  • I have already seen this but as you see my code has var xDoc = XDocument.Load(kmlFile); to load the file and then query on xDoc. How do I get the xDoc? – user1254053 Jan 31 '18 at 10:46
  • this answer can be applied to your case: https://stackoverflow.com/a/20662863/1537726 – csharpwinphonexaml Jan 31 '18 at 12:44
  • @csharpwinphonexaml - no it can not unless I have a string and not a file – user1254053 Feb 01 '18 at 07:33
  • strXmlFile in the answer I linked you to is a file path not the content of the xml file – csharpwinphonexaml Feb 01 '18 at 12:13
  • @csharpwinphonexaml - XmlReader reader = XmlReader.Create(strXmlFile, settings, context); yes this takes the xml file name but it return the XMLDocument and I want XDocument..because I have to run this code also xDoc.Root .Elements(ns + "Document") .Elements(ns + "Placemark") .GroupBy(i => (string)i) .SelectMany(g => g.Skip(1)) .Remove(); – user1254053 Feb 01 '18 at 12:23
  • @rene The file is a valid google earth KML file in XML format. It does open up correctly in Google earth. – user1254053 Feb 01 '18 at 12:44
  • `var xdoc = XDocument.Load(reader);` will give you the XDocument – rene Feb 01 '18 at 15:21
  • @user1254053 first fix your xml and save it then load it with XDocument. – csharpwinphonexaml Feb 01 '18 at 16:01

0 Answers0