1

I have the following xml file:

<LabelImageCreator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <PrintFieldList>
    <PrintFieldDefinition>
      <FieldName>Facility</FieldName>
      <DataParameterName>Address</DataParameterName>
      <FieldFont>
        <FontName>Arial</FontName>
        <FontSize>10</FontSize>
        <FontStyle>Regular</FontStyle>
      </FieldFont>
      <CurrentDataValue/>
    </PrintFieldDefinition>
    <PrintFieldDefinition>
      <FieldName>Country</FieldName>
      <DataParameterName>CountryofOrigin</DataParameterName>
      <WrapField>false</WrapField>
      <FieldFont>
        <FontName>Arial</FontName>
        <FontSize>8</FontSize>
        <FontStyle>Regular</FontStyle>
      </FieldFont>
      <CurrentDataValue/>
      <TextPrefix>Produce of </TextPrefix>
    </PrintFieldDefinition>
  <PrintFieldList>
<LabelImageCreator>

I have to select the attribute with field name Facilityand add the address(eg: No 2546, Gorrge street, California, US) to <CurrentDataValue/> field and save it.

I tried with the below code,

 XmlDocument xmlDocument = new XmlDocument();
  xmlDocument.Load(path);
  var node = xmlDocument.DocumentElement.SelectSingleNode(
             "./PrintFieldList/PrintFieldDefinition[@FieldName='Facility']");

Above code while debuging it is not working. Can any one guide me how to select and update the xml attribute.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
user2086641
  • 4,331
  • 13
  • 56
  • 96

2 Answers2

2

A couple of minor issues:

  • You need to start from the root element LabelImageCreator
  • FieldName is an element, not an attribute, so hence FieldName and not @FieldName
  • The closing tags on the Xml Document don't match up.

If you want to select the child element CurrentDataValue of parent PrintFieldDefinition with the child FieldName with value Facility:

var node = xmlDocument.DocumentElement.SelectSingleNode(
"/LabelImageCreator/PrintFieldList/PrintFieldDefinition[FieldName='Facility']/CurrentDataValue");

Changing the value is then simply:

node.InnerText = "No 2546, Gorrge street, California, US";      
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Your answer is working fine. Can you also guide me how to update the field and save back to the same xml file. – user2086641 Nov 28 '17 at 18:58
  • With XmlDocument, you can use `InnerText` or `InnerXml` to change the value, or mutate the xml tree. Saving is as easy as [`xmlDocument.Save(fileName)`](https://msdn.microsoft.com/en-us/library/dw229a22%28v=vs.110%29.aspx) Alejandro has a point, though - the more recent `XDocument` should be given preference. You can still all the power of [`XPath 1.0`](https://stackoverflow.com/a/27583026/314291) with XDocument. – StuartLC Nov 28 '17 at 19:03
  • @user2086641 Apologies - I missed the second part of your question, re editing the `CurrentDataValue` node - I've updated - you can select the child `CurrentDataValue` node of `PrintFieldDefinition` based on the value of another child `FieldName` node in `XPath` – StuartLC Nov 28 '17 at 19:19
  • No problem, I googled with the help your last comment and fixed it. Thanks for your help – user2086641 Nov 28 '17 at 19:31
1

I would use XDocument instead of XmlDocument (it allows you to use linq which in my opinion, is easier than using xpath).

You can find your node like this and I believe you can update them too (first search and get the value, then search again and update on the other node).

Example:

var nodesMatching = from node in myXDocument.Descendants()
where node.Name.LocalName.Equals("mySearchNode")
select node;
var node = nodesMatching.FirstOrDefault();