0

如何使用 c# 在以下 xml 文件中更改 sourcePatientInfo 的值。我可以使用读取值,

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

如何使用 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>

我想使用 c# 更改值。我无法找出路。任何解决此问题的帮助将不胜感激。

我想改变

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

到以下值

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

我也尝试过以下代码,

 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";
    }
  }

在此代码中,nodelist.count 始终为零:-(。请帮助我解决问题。

4

2 回答 2

2

如果您需要更新前两个值:

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

如果您必须按值搜索,您可以执行以下操作:

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;
 }

并在某些功能内

 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");

调用 xml 时的 UPDxml.Descendants("Slot")仅查找默认命名空间中的元素。我使用扩展方法作为一种快速解决方法来避免这种情况:

public static IEnumerable<XElement> NsDescendants(this XContainer e, string elementName)
{
    return e.Descendants().Where(d => d.Name.LocalName == elementName);
}
于 2013-09-17T07:33:11.810 回答
0

最后我的问题用下面的代码解决了。

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");
于 2013-09-17T12:47:45.617 回答