bushin Posted November 2, 2015 at 02:03 PM Report Share #589414 Posted November 2, 2015 at 02:03 PM 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 More sharing options...
apocsantos Posted November 2, 2015 at 02:44 PM Report Share #589419 Posted November 2, 2015 at 02:44 PM 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 More sharing options...
He B TeMy Posted November 2, 2015 at 03:12 PM Report Share #589423 Posted November 2, 2015 at 03:12 PM http://stackoverflow.com/questions/2558787/how-to-modify-existing-xml-file-with-xmldocument-and-xmlnode-in-c-sharp Link to comment Share on other sites More sharing options...
bioshock Posted November 2, 2015 at 03:15 PM Report Share #589425 Posted November 2, 2015 at 03:15 PM http://stuffpinho.com/csharp-simplificar-xml/ Link to comment Share on other sites More sharing options...
bushin Posted November 2, 2015 at 09:19 PM Author Report Share #589445 Posted November 2, 2015 at 09:19 PM (edited) 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 November 2, 2015 at 09:21 PM by bushin Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now