Jump to content

Editar ficheiro XML - xPath?


bushin
 Share

Recommended Posts

Bom dia,

conseguem ajudar, tenho o seguinte requisito:

Editar ficheiro XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<connections xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<connection id="1" name="SqlServerConnect" type="100" refreshedVersion="5" savePassword="0" saveData="1">
<extLst>
 <ext uri="{HE250138-89BD-433C-8126-D09CA5730AF9}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
 <x15:connection id="123f">
	 <x15:oledbPr connection="Provider=SQLNCLI11;Data Source=102.109.01.254;Initial Catalog=dbDEV;User ID=007;Persist Security Info=false;Password=TempPass">
	 <x15:dbCommand text="Select COUNT(1) VOLUME_FROM dbDEV.dbo.vw_DevTest01" />
	 </x15:oledbPr>
 </x15:connection>
 </ext>
</extLst>
</connection>
<connection id="2" keepAlive="1" name="ThisWorkbookDataModel" description="Data Model" type="5" refreshedVersion="5" minRefreshableVersion="5" background="1">
<dbPr connection="Data Model Connection" command="Model" commandType="1" />
<olapPr sendLocale="1" rowDrillCount="1000" />
<extLst>
 <ext uri="{HE250138-89BD-433C-8126-D09CA5730AF9}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
 <x15:connection id="" model="1" />
 </ext>
</extLst>
</connection>
</connections>

O objetivo é conseguir alterar o XML no registo "<x15:oledbPr connection=..:"., mas só estou a conseguir chegar ao Root node.

var xDoc = Xdocument.Load(@"C:\Users\Dev\connections.xml");
var ns = XNamespace.Get("http://schemas.openxmlformats.org/spreadsheetml/2006/main");
var connSqlServer = xDoc.Root.Elements(ns + "connection")
	 .FirstOrDefault(x => (int)x.Attribute("id") == 1)
		 .Attribute("savePassword");
connSqlServer.Value = "0";
xDoc.Save(@"C:\Users\Dev\tempConnection.xml");

Alguma dica?

Obrigada

Link to comment
Share on other sites

Boa tarde,

Podes fazer isso com um ciclo for, iterar os elementos, usar uma condição para validar se o elemento é o que queres, e aí atribuir à variável o/os valor(es) do(s) elemento(s), que queres.

Cordiais cumprimentos,

Apocsantos

"A paciência é uma das coisas que se aprendeu na era do 48k" O respeito é como a escrita de código, uma vez perdido, dificilmente se retoma o habito"

Link to comment
Share on other sites

Obrigada pelo feedback.

Os nodes a percorrer são:

// Extrutura XML
< connections xmlns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main" >
  < connection id = "1" name = "SqlServerConnect"
      < extLst >
< ext uri = "{HE250138-89BD-433C-8126-D09CA5730AF9}" xmlns:x15 = "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" >
< x15:connection id = "123f" >
< x15:oledbPr connection = "Provider=SQLNCLI11;Data Source=102.109.01.254;Initial Catalog=dbDEV;User ID=007;Persist Security Info=false;Password=TempPass" >
< x15:dbCommand text = "Select COUNT(1) VOLUME_FROM dbDEV.dbo.vw_DevTest01" />

Antes de fazer um ciclo for, para iterar os elementos, estou a tentar chegar a um determinado node, utilizando os diversos NamesSpace, mas sem sucesso no preenchimento da variável nodes.

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Users\Dev\connections.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
ns.AddNamespace("x15", "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main");

XmlNodeList nodes = doc.SelectNodes("/x:connection/x:connection[@id='1']/extLst/x15:connection/x15:oledbPr", ns);
Edited by bushin
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.