I'm trying to read a GPX-file (a kind of XML file for location data). This is the structure:
<?xml version="1.0"?>
<gpx creator="GPX-service" version="1.1"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd">
<trk>
<name>Route</name>
<trkseg>
<trkpt lat="51.966738" lon="6.501578">
</trkpt>
<trkpt lat="51.966689" lon="6.501456">
</trkpt>
</trkseg>
</trk>
</gpx>
I'v readed in more than hundred XML-files in the past, but this one will not work. I'm reading the GPX-file in this way:
XmlDocument gpxDoc = new XmlDocument();
gpxDoc.Load(gpxfile);
XmlNodeList nl = gpxDoc.SelectNodes("trkpt");
foreach (XmlNode xnode in nl)
{
string name = xnode.Name;
}
Variable 'gpxfile' is the path to the gpxfile, which is correct (tested).
You need to work with namespaces. The element trkpt does not exist in the current context, only in the namespace http://www.topografix.com/GPX/1/1. Here's an example how you work with said namespaces - let x be an alias to the URI.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(gpxDoc.NameTable);
nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/1");
XmlNodeList nl = gpxDoc.SelectNodes("//x:trkpt", nsmgr);
Note that we select nodes in the x namespace now (e.g. //x:trkpt instead of //trkpt).
Related
SelectNodes always returning the 0 count even the it has the value.
<?xml version="1.0" encoding="utf-16"?>
<Configurations xmlns="DEH_Common.Schemas">
<sftpConfiguration>
<file>
<filedetails>
<fileext>csv</fileext>
<DataContentDetailId>1</DataContentDetailId>
</filedetails>
</file>
</sftpConfiguration>
</Configurations>
C# to read the nodelist....
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("//Configurations/sftpConfiguration/file");
It's because of using namespace in your xml, You should add namespace to xmlDoc and also no need to useDocumentElement` This code would be work:
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("a", "DEH_Common.Schemas");
XmlNodeList nodeList = xmlDoc.SelectNodes("//a:sftpConfiguration/a:file", nsmgr);
In your XML DocumentElement is the Configurations node, so your XPath should be sftpConfiguration/file
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("sftpConfiguration/file");
Try this:
xmlDoc.DocumentElement.SelectNodes("/Configurations[#*]/sftpConfiguration/file");
I need some help on how to get values of English and studentId from this XML. Maybe it needs some special parsing I can't figure out at the moment.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<processResponse xmlns="http://tempuri.org/">
<processResult>
<xml version="1.0" encoding="UTF-8"?>
<returnedResponse>
<English>94</English>
<Remarks>Excellent</Remarks>
<studentId>tst005</studentId>
<Department>6</Department>
</returnedResponse>
</processResult>
</processResponse>
</soap:Body>
</soap:Envelope>
I have tried the following code:
XmlDocument xdr = new XmlDocument();
xdr.LoadXml(thexml);
XmlNodeList nodelist = xdr.SelectNodes("//processResponse/processResult/returnedResponse");
foreach (XmlNode node in nodelist)
{
string eng = node.SelectSingleNode("English").InnerText;
Response.Write("eng");
}
Do you get an error with invalid XML or does your SelectNodes not find anything?
How about this path
XmlNodeList nodelist = xdr.SelectNodes("/*/*/*/processResponse/processResult/returnedResponse");
You can try with SelectSingleNode, just like this.
var English= xdr.DocumentElement.SelectSingleNode("English").InnerText;
var studentId = xdr.DocumentElement.SelectSingleNode("studentId").InnerText;
1) XML ist invalid at line 6 -> because it does not have a closing tag. If you cannot edit the Service and strip the xml header or encode the resulting xml, you have to strip the line 6 in you consumercode.
2) You have to define the Namespaces!
Btw: I highly recommend using (Linq2XML) XDocument. Your code could look like this:
XDocument xdr = XDocument.Parse(...);
var nodes = xdr.XPathSelectElements("//*[name()='returnedResponse']");
Try this
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XmlNodeList nList = doc.GetElementsByTagName("returnedResponse");
foreach (XmlNode node in nList)
{
XElement xelement = XElement.Parse(node.OuterXml);
var Descendents = xelement.Descendants();
foreach (var item in Descendents)
{
//you'll get each of your descendents here
}
}
I'm writing on a program in C# that'll take data from a server, write it into a file and then read the file to grab the data contained.
The last part is the problem. I get my XML-file stream and the XML file seems to be fine but except for the root node, my program doesn't accept any child nodes even though in a list it counts all the 700 child nodes. Was checking my spelling, the adressing, the tree... nothing worked so far.
XML Data:
<?xml version="1.0" encoding="UTF-8"?>
<uniprot xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://uniprot.org/uniprot http://www.uniprot.org/support/docs/uniprot.xsd">
<entry dataset="Swiss-Prot" created="2005-03-01" modified="2013-10-16" version="51">
<accession>P69430</accession>
<accession>O65938</accession>
<accession>P27856</accession>
<name>TATA_ECO57</name>
<protein>
<recommendedName>
<fullName>Sec-independent protein translocase protein TatA</fullName>
</recommendedName>
</protein>
program:
Datapath = startupPath + "\\" + Data[0, 0] + ".xml";
XmlDocument XMLdoc = new XmlDocument();
XMLdoc.Load(Datapath);
XmlNodeList xnList = XMLdoc.SelectNodes("//*"); //the list shows all 700 entries so the path etc are correct
var node = XMLdoc.SelectSingleNode("uniprot/entry/protein/recommendedName/fullName").InnerText;
As soon as I try to adress any child node from uniprot, it's just null which drives me mad. Anyone can help please?
Have you tried using the namespace?
XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "http://uniprot.org/uniprot");
var node = doc.SelectSingleNode("//ns:exampleNode", nsmgr);
Try this one: XmlNode node = doc.SelectSingleNode("//uniprot//entry/protein/recommendedName /fullName");
node.InnerXml;
I am trying to edit a xml file.
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(#"C:\\Users\\Vahid\\Desktop\\HG\\HG\\HG\\singleM.kml");
XmlNode myNode = myXmlDocument.SelectSingleNode(
"/kml/Document/Placemark/Point/coordinates");
myNode.Value = coordinates;
myXmlDocument.Save(#"C:\\Users\\Vahid\\Desktop\\HG\\HG\\HG\\singleM.kml");
and this is my xml (.kml) file:
<?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">
<Document>
<name>change.kml</name>
<Style id="sn_ylw-pushpin"></Style>
<Placemark>
<Point>
<coordinates>0, 0,0</coordinates>
</Point>
<name>12</name>
</Placemark>
</Document>
</kml>
Xml namespaces:
XmlNamespaceManager ns = new XmlNamespaceManager(myXmlDocument.NameTable);
ns.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
XmlNode myNode = myXmlDocument.SelectSingleNode("/kml:kml/kml:Document/kml:Placemark/kml:Point/kml:coordinates", ns);
myNode.InnerText = coordinates;
Note that there is nothing special about "kml" / "kml:" here - it could just as well be:
XmlNamespaceManager ns = new XmlNamespaceManager(myXmlDocument.NameTable);
ns.AddNamespace("x", "http://www.opengis.net/kml/2.2");
XmlNode myNode = myXmlDocument.SelectSingleNode("/x:kml/x:Document/x:Placemark/x:Point/x:coordinates", ns);
myNode.InnerText = coordinates;
The important point is that each of your elements are in the namespace http://www.opengis.net/kml/2.2; the AddNamespace is just adding an alias for that, so that we can talk about the namespace conveniently - we then write the xpath using the alias, and pass the namespace-manager to the SelectSingleNode methods.
I have an XML file that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.someurl.com/somefile.xslt"?>
<AutoInsuranceClaim xmlns="http://www.someurl.com/schemas/AutoInsuranceClaim">
<Identification>
<BaseOwner>3</BaseOwner>
<BaseType>ABC123</BaseType>
<BaseTypeRef>471038341757</BaseTypeRef>
</Identification>
</AutoInsuranceClaim>
and I'm trying to read the Identification node. Here's my code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"..\..\Data.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://www.someurl.com/schemas/AutoInsuranceClaim");
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/Identification", nsmgr);
Console.WriteLine("There are {0} nodes...", nodeList.Count);
I know I should get a least 1 value. My understanding of .NET XML parsing is that if you have a default namespace with no prefix, you have to create your own namespace. But this should have returned 1.
If not, what am I missing?
I might be grasping at straws here, but shouldn't you be namespacing both entities in your xpath expression?
XmlNodeList nodeList = xmlDoc.SelectNodes(#"/ns:AutoInsuranceClaim/ns:Identification", nsmgr);
XElement root = XElement.Load("Data.xml");
var identifications = root.Descendants()
.Where(x => x.Name.LocalName == "Identification")
.ToList()
The problem is that you're trying to find an Identification node without a namespace, but it will have defaulted to the same namespace as the parent due to the xmlns=... part. Try this:
var nodeList = xmlDoc.SelectNodes("/ns:AutoInsuranceClaim/ns:Identification",
nsmgr);
Having tried it myself, it printed a count of 1.
Personally I'd use LINQ to XML instead though, which makes namespace easier handling:
XDocument doc = XDocument.Load(#"..\..\Data.xml");
XNamespace ns = "http://www.someurl.com/schemas/AutoInsuranceClaim";
var nodes = doc.Root.Elements(ns + "Identification");