El TecnoBaúl de Kiquenet

Kiquenet boring stories

Posts Tagged ‘xmldocument’

Modify machine.config (config files) using Powershell

Posted by kiquenet en 22 enero 2015

ConfigurationManager

$config = [System.Configuration.ConfigurationManager]::OpenMachineConfiguration() 

"{0,-25}     {1,25}"  -f "   Name", "   Allow Definition"
"{0,-25}     {1,25}"  -f "   —-", "   —————-"
$i = 0 

foreach ($section in $config.Sections) 

     "{0,-25}     {1,25}" -f $section.SectionInformation.Name,$section.SectionInformation.AllowExeDefinition 
     $i++ 

"Total number of sections: {0}" -f $i

get-content and DocumentElement

$xml = [xml](get-content $machineConfig)
$xml.Save($machineConfig + "_$currentDate")
$root = $xml.get_DocumentElement()
$system_web = $root."system.web"
if ($system_web.machineKey -eq $nul) {
$machineKey = $xml.CreateElement("machineKey")
$a = $system_web.AppendChild($machineKey)
}
$system_web.SelectSingleNode("machineKey").SetAttribute("validationKey","$validationKey")
$system_web.SelectSingleNode("machineKey").SetAttribute("decryptionKey","$decryptionKey")
$system_web.SelectSingleNode("machineKey").SetAttribute("validation","$validation")
$a = $xml.Save($machineConfig)

XmlDocument

if (!$origXml.configuration.’system.serviceModel’.bindings)
{
    $tempXmlDoc = new-object System.Xml.XmlDocument
    $tempXmlDoc.LoadXml($newXml)
    $newNode = $origXml.ImportNode($tempXmlDoc.DocumentElement, $true)
    $origXml.configuration.’system.serviceModel’.AppendChild($newNode)
}

Sources:
http://sunauskas.com/blog/edit-config-file-with-powershell/
http://geekswithblogs.net/nharrison/archive/2011/05/25/updating-the-machine.config–with-powershell.aspx

http://pshscripts.blogspot.com.es/2009/02/get-machineconfig.html
http://jeffgraves.me/2012/06/05/read-write-net-machine-key-with-powershell/
http://stackoverflow.com/questions/10342657/how-can-i-add-a-section-to-a-web-config-using-powershell

Anuncios

Posted in PowerShell, Scripts | Etiquetado: , , | Leave a Comment »

XmlDocument vs XDocument in C#

Posted by kiquenet en 23 septiembre 2014

Attention:

Ensure Encoding ANSI not UTF-8 (view using Notepad++ option)

Xml Namespace (ns)

Using

using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;


var xml = @"           
<Project xmlns=""
http://schemas.microsoft.com/developer/msbuild/2003"">
  <PropertyGroup>
    <MfaAvisosComoErrores>False</MfaAvisosComoErrores>
    <MfaEntorno>Desarrollo</MfaEntorno>
  </PropertyGroup>
</Project>";

            var xml2 = @"           
<Project>
  <PropertyGroup>
    <MfaAvisosComoErrores>False</MfaAvisosComoErrores>
    <MfaEntorno>Desarrollo</MfaEntorno>
  </PropertyGroup>
</Project>";

XmlDocument

var xDoc1 = new System.Xml.XmlDocument();
xDoc1.Load(f1);

var nodes = xDoc1.GetElementsByTagName("MfaAvisosComoErrores");
Assert.AreNotEqual(0, nodes.Count);
Assert.AreEqual("False", nodes[0].InnerText, "MfaAvisosComoErrores");

XDocument withouth namespace

var doc2 = XDocument.Parse(xml2);
Assert.AreNotEqual(0, doc2.Descendants("MfaAvisosComoErrores").ToList().Count);

XDocument with namespace

XNamespace ns = http://schemas.microsoft.com/developer/msbuild/2003;
var doc1 = XDocument.Parse(xml);
Assert.AreNotEqual(0, doc1.Descendants(ns + "MfaAvisosComoErrores").ToList().Count);
Assert.AreNotEqual(0, doc1.Root.Descendants(ns + "MfaAvisosComoErrores").ToList().Count);

From file

                        var doc = XDocument.Load(f1);
            Assert.AreEqual(0, doc.Descendants("MfaAvisosComoErrores").ToList().Count);

            var MfaAvisosComoErrores = doc.Element(ns + "Project").Element(ns + "PropertyGroup").Element(ns + "MfaAvisosComoErrores").Value;
            Assert.AreEqual("False", MfaAvisosComoErrores, "MfaAvisosComoErrores");
            var MfaEntorno = doc.Element(ns + "Project").Element(ns + "PropertyGroup").Element(ns + "MfaEntorno").Value;
            Assert.AreEqual("Desarrollo", MfaEntorno, "MfaEntorno");

            var query = from property in doc1.Root.Descendants(ns + "PropertyGroup")
                        let MfaAvisosComoErroresValue = (string)property.Element(ns + "MfaAvisosComoErrores")
                        where MfaAvisosComoErroresValue == "False"
                        select new
                        {
                            MfaAvisosComoErrores = MfaAvisosComoErroresValue,
                        };
            Assert.AreNotEqual(0, query.ToList().Count);

References:
http://stackoverflow.com/questions/3460991/selecting-a-xelement-from-a-xdocument
http://stackoverflow.com/questions/566167/query-an-xdocument-for-elements-by-name-at-any-depth
http://stackoverflow.com/questions/15099197/xdocument-to-list-of-object
http://stackoverflow.com/questions/8460464/finding-element-in-xdocument

Posted in .NET | Etiquetado: , , | Leave a Comment »