首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C#在XML中设置值?

如何使用C#在XML中设置值?
EN

Stack Overflow用户
提问于 2013-09-17 06:16:05
回答 2查看 195关注 0票数 0

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

代码语言:javascript
复制
var elem = (from n in xml.Descendants("Slot")
                        where n.Attribute("name").Value == "sourcePatientInfo"
                        select n).FirstOrDefault();

如何使用C#更改相同的内容?

代码语言:javascript
复制
<?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#更改值。我找不出路来。如能帮助解决这一问题,将不胜感激。

我想换一下

代码语言:javascript
复制
 <Slot name="sourcePatientInfo">
 <ValueList>
 <Value>PID-3|pid1^^^&amp;1.2.3&amp;ISO</Value> 
 <Value>PID-5|Doe^John^^^</Value> 

为下列值

代码语言:javascript
复制
 <Slot name="sourcePatientInfo"> 
 <ValueList> <Value>PID-3|MyPID</Value> 
 <Value>PID-5|MyName</Value>

我也试过以下代码,

代码语言:javascript
复制
 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总是为零:-(。请帮我解决这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-17 12:47:45

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

代码语言:javascript
复制
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");
票数 0
EN

Stack Overflow用户

发布于 2013-09-17 07:33:11

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

代码语言:javascript
复制
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

如果必须按值进行搜索,则可以:

代码语言:javascript
复制
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;
 }

和内部的一些功能

代码语言:javascript
复制
 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调用UPD时,只需查找默认命名空间中的元素。为了避免这种情况,我使用扩展方法作为一种快速解决方法:

代码语言:javascript
复制
public static IEnumerable<XElement> NsDescendants(this XContainer e, string elementName)
{
    return e.Descendants().Where(d => d.Name.LocalName == elementName);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18842531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档