Problemas al leer el archivo xml
Frecuentes
Visto 1,452 veces
1
I have a problem with reading XML which looks as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<connections xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<connection id="1" odcFile="C:\Users\andr\Documents\My Data Sources\ELITE_MSSQLSERVER2012 PSO Reports.odc" keepAlive="1" name="ELITE_MSSQLSERVER2012 PSO Reports" type="5" refreshedVersion="5" background="1">
<dbPr connection="Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=PSO;Data Source=ELITE\MSSQLSERVER2012;MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error" command="Reports" commandType="1"/>
<olapPr sendLocale="1" rowDrillCount="1000"/>
</connection>
</connections>
(it's actually a connection.xml file in *.xlsx package). I'm trying to read it with the help of XDocument. I need to change the "odcFile" property. The problem is that it reads the whole thing as a single element without children elements. Why so? XML looks normal. Or maybe there is another workaround to make this?
2 Respuestas
1
Try using System.Xml.Document instead, if you can access an actual xml file. Not sure what you mean by "it's actually a connection.xml file in *.xlsx package":
using System.Xml
XmlDocument dom = new XmlDocument();
XmlNone root;
dom.Load(fullpath);
root = dom.DocumentElement;
Respondido el 10 de Septiembre de 13 a las 00:09
Thanks, this way everything works. But that's kind of strange, as the XDocument is not a third party thing, why it's so buggy then? And as for the *.xlsx package - I'm unzipping the Excel file to dynamicaly change its connection properties. - KorsaR
Linq to Xml has many advantages though, you can see an overview aquí. And i don't believe it's a bug, you're probably not using XNamespace
correctly :). - UIlrvnd
@KorsaR - naming something "buggy" just because it follows public specification may be overkill. Consider reading about Xml namespaces... - Alexéi Levenkov
If it works, go for it, XDocument works well with Linq but if you don't need or prefer to do Linq queries against the loaded Xml just use System.Xml.Document instead. It is possible that it is some kind of bug that System.Xml.Document correctly parses the xml into its parent and child nodes but XDocument does not. But again if you don't have a need to query the loaded Xml with Linq then just go with what works until requirements change. - brian ogden
1
You have to use proper XNamespace
instance when querying this document. That's because your <connections>
tag sets the default namespace to "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
.
var xDoc = XDocument.Load("Input.txt");
var ns = XNamespace.Get("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
var odcFile = xDoc.Root.Elements(ns + "connection")
.FirstOrDefault(x => (int)x.Attribute("id") == 1)
.Attribute("odcFile");
odcFile.Value = "newOdcValue";
xDoc.Save("Input.txt");
Respondido el 10 de Septiembre de 13 a las 19:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# xml linq-to-xml or haz tu propia pregunta.
What is "it" in "it reads the whole thing"??? Where is the code that you have problem with? - Alexei Levenkov
"it" means XDocument. I tried var doc = XDocument.Load(connectionsFilePath); var connection = doc.Descendants("connection").First(); - KorsaR
Entonces esto no funciona:
d.Element(System.Xml.Linq.XNamespace.Get("http://schemas.openxmlformats.org/spreadsheetml/2006/main") + "connections").Element(System.Xml.Linq.XNamespace.Get("http://schemas.openxmlformats.org/spreadsheetml/2006/main") + "connection").Attribute("odcFile").SetValue("value");
(where d is theXDocument
and value is the value you want to changeodcFile
para)? - UIlrvndposible duplicado de Use Linq to Xml con espacios de nombres Xml or many other "select node with non-default namespace" question. - Alexei Levenkov