Dineshkumar
Dineshkumar

Reputation: 1548

How to set a value in XML using C#?

How to change the value of sourcePatientInfo in the following xml file using c#. I can able to read the value using,

var elem = (from n in xml.Descendants("Slot")
                        where n.Attribute("name").Value == "sourcePatientInfo"
                        select n).FirstOrDefault();

How to change the same using C#?

<?xml version="1.0" encoding="utf-8"?>
<rs:SubmitObjectsRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:rs="urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1" xmlns:rim="urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.1" xmlns="urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.1">
<LeafRegistryObjectList>
<ObjectRef id="urn:uuid:93606bcf-9494-43ec-9b4e-a7748d1a838d" />
<ExtrinsicObject id="Document01" mimeType="application/dicom" objectType="urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1">
  <Name>
    <LocalizedString value="Physical" />
  </Name>
  <Description />         
  <Slot name="sourcePatientId">
    <ValueList>
      <Value>pid1^^^&amp;1.2.3&amp;ISO</Value>
    </ValueList>
  </Slot>
  <Slot name="sourcePatientInfo">
    <ValueList>
      <Value>PID-3|pid1^^^&amp;1.2.3&amp;ISO</Value>
      <Value>PID-5|Doe^John^^^</Value>
      <Value>PID-7|19560527</Value>
      <Value>PID-8|M</Value>
      <Value>PID-11|100 Main St^^Metropolis^Il^44130^USA</Value>
    </ValueList>
  </Slot>

I would like to change the values using c#. Am not able to figure out the way. Any Help to resolve this issue will be appreciated.

I want to change the

 <Slot name="sourcePatientInfo">
 <ValueList>
 <Value>PID-3|pid1^^^&amp;1.2.3&amp;ISO</Value> 
 <Value>PID-5|Doe^John^^^</Value> 

to the following value

 <Slot name="sourcePatientInfo"> 
 <ValueList> <Value>PID-3|MyPID</Value> 
 <Value>PID-5|MyName</Value>

I have also tried the following the code,

 XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc1.NameTable);
 namespaceManager.AddNamespace("rs", "urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1");
 var query = "/rs:SubmitObjectsRequest/LeafRegistryObjectList/ExtrinsicObject";
 XmlNodeList nodeList = xmlDoc1.SelectNodes(query, namespaceManager);

 foreach (XmlNode node1 in nodeList)
 {
   if (node1.Attributes["Slot"].Value == "sourcePatientInfo")
   {
      node1.Attributes["ValueList"].Value = "Myvalue";
    }
  }

In this code, nodelist.count is always zero :-(. Kindly help me to resolve the issue.

Upvotes: 0

Views: 203

Answers (2)

Dineshkumar
Dineshkumar

Reputation: 1548

Finally my problem is solved with the following code.

XmlDocument xmlDocSOR = new XmlDocument();
XmlDocSOR.Load("filename.xml");
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocSOR.NameTable);
namespaceManager.AddNamespace("rs", "urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1");
namespaceManager.AddNamespace("ns", "urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.1");
var query = "/rs:SubmitObjectsRequest/ns:LeafRegistryObjectList/ns:ExtrinsicObject/ns:Slot";
XmlNodeList nodeList = xmlDocSOR.SelectNodes(query, namespaceManager);

foreach (XmlNode plainnode in nodeList)
{
    if (plainnode.Attributes["name"].Value == "sourcePatientId")
    {
        XmlNode childnode = plainnode.LastChild;
        XmlElement ee1 = (XmlElement)childnode.FirstChild;
        ee1.InnerText = sPatientID;                      
     }
 }
 xmlDocSOR.Save("filename.xml");

Upvotes: 0

Pavel Murygin
Pavel Murygin

Reputation: 2332

If you need to update first two values:

var slot = xml.Descendants("Slot")
              .Where(n => n.Attribute("name").Value == "sourcePatientInfo")
              .FirstOrDefault();
if(slot == null)                  
{
    throw new WhateverAppropriateHereEcxeption();
}
var values = slot.Descendants("Value").ToList();
if(values.Count < 2)                  
{
    throw new WhateverAppropriateHereEcxeption();
}
values[0].Value = "PID-3|MyPID"  // updating the first value
values[1].Value = "PID-5|MyName" // updating the second value

if you have to search by value you can do:

bool UpdateValue(XElement slot, string oldValue, string newValue)
{
    var elem = slot.Descendants("Slot")
                   .Where(n => n.Name == "Value" && n.Value == oldValue)
                   .FirstOrDefault();
    if(elem != null)
    {
        elem = newValue;
        return true;
    }

    return false;
 }

and inside some function

 var slot = xml.Descendants("Slot")
               .Where(n => n.Attribute("name").Value == "sourcePatientInfo")
               .FirstOrDefault();
 if(slot == null)                  
 {
     throw new WhateverAppropriateHereEcxeption();
 }
 UpdateValue(slot, "PID-3|pid1^^^&amp;1.2.3&amp;ISO", "PID-3|MyPID");
 UpdateValue(slot, "PID-5|Doe^John^^^", "PID-5|MyName");

UPD when you call xml.Descendants("Slot") xml look only for elements in default namespace. I use an extension method as a quick workaround to avoid that:

public static IEnumerable<XElement> NsDescendants(this XContainer e, string elementName)
{
    return e.Descendants().Where(d => d.Name.LocalName == elementName);
}

Upvotes: 2

Related Questions