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.

2 Answers 2

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";      
Sign up to request clarification or add additional context in comments.

4 Comments

Your answer is working fine. Can you also guide me how to update the field and save back to the same xml file.
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) Alejandro has a point, though - the more recent XDocument should be given preference. You can still all the power of XPath 1.0 with XDocument.
@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
No problem, I googled with the help your last comment and fixed it. Thanks for your help
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.