tags:

views:

48

answers:

3

I have the following text in xml file:

<Config Builder="LP Wizard">
    <Libraries>
        <Library Name="XCAMSource"/>
    </Libraries>
    <InputFormats>
        <XCAM Format="XCAM" LibraryDirectory="C:\XCAM"/>
    </InputFormats>
    <OutputFormats>
        <Pads Version="PADS 5.0" ExportAscii="false" LibraryGenerate="true" ExtendedLayers="false" AlphaLoc="PART TYPE" Format="PADS" LibraryDirectory="c:\XCAM\OUTPUT" DirectoryStructure="false" Units="Millimeters" NewCodeVersion="false" usrLayerNameElecT="1" usrLayerNameElecB="2" usrLayerNameSilk="26" usrLayerNameSilkb="29" usrLayerNameCY="20" usrLayerName3D="25" usrLayerNameAssy="27" usrLayerNameAssyb="30" usrLayerNamePmask="23" usrLayerNameSmask="21" usrLayerNameSmaskb="28" DirectImport="false"/>
    </OutputFormats>
</Config>

I need to change the text "C:\XCAM" that sits under LibraryDirectory=.

What is the smart way to do so, I just dont want to string search for LibraryDirectory= and then search for first and last " and then to change the text betweenthem.

A: 

You could load the file into an XmlDocument, select the attribute using XPath, and write it back out. This is a lot more complex, but probably the "right" solution for production code.

If you have multiple files or multiple occurrences to replace, a RegEx might be an easier option - this would likely be faster and shorter code, but it's not as descriptive. If what you're doing is (say) writing a tool for non-production use (say, to transform a bunch of config files for use on different machines) this is probably reasonable.

Dan Puzey
-1: never use regular expressions to search XML or HTML. They are not regular languages, and regular expressions cannot describe them.
John Saunders
+4  A: 
var doc = XDocument.Load("test.xml");
doc.Root.Element("XCAM").Attribute("LibraryDirectory").Value = "new value";
doc.Save("test.xml");

UPDATE:

doc.Root
   .Element("InputFormats")
   .Element("XCAM")
   .Attribute("LibraryDirectory").Value = "new value";

or using XPATH:

doc.XPathSelectElement("//InputFormats/XCAM")
   .Attribute("LibraryDirectory").Value = "new value";

Don't forget to add the using System.Xml.XPath as XPathSelectElement is an extension method.

Darin Dimitrov
I didnt paste all the xml file before can you help me please with the fixed one please.
Night Walker
Please see my update.
Darin Dimitrov
A: 

You could pass the XML trough this basic XSL stylesheet:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>
  <!-- the identity template copies everything verbatim -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>
  <!-- only LibraryDirectory attributes get a new value -->
  <xsl:template match="@LibraryDirectory">
    <xsl:copy>
      <xsl:value-of select="'the text here to be changed'" />
    </xsl:copy>
  </xsl:template>
</xsl:stylessheet>

To apply a stylesheet to your XML document, you could use the XslCompiledTransform class.

Tomalak