Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#在XML中设置值?_C#_Xml - Fatal编程技术网

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

如何使用C#在XML中设置值?,c#,xml,C#,Xml,如何使用c#更改以下xml文件中sourcePatientInfo的值。 我可以使用 var elem = (from n in xml.Descendants("Slot") where n.Attribute("name").Value == "sourcePatientInfo" select n).FirstOrDefault(); 如何使用C#更改相同的内容 在此代码中,nodelist.

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

var elem = (from n in xml.Descendants("Slot")
                        where n.Attribute("name").Value == "sourcePatientInfo"
                        select n).FirstOrDefault();
如何使用C#更改相同的内容


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

如果需要更新前两个值:

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
在一些函数中

 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.subjections(“Slot”)
xml时,UPD只查找默认命名空间中的元素。我使用扩展方法作为快速解决方法,以避免:

public static IEnumerable<XElement> NsDescendants(this XContainer e, string elementName)
{
    return e.Descendants().Where(d => d.Name.LocalName == elementName);
}
public静态IEnumerable子体(此XContainer e,string elementName)
{
返回e.subjects(),其中(d=>d.Name.LocalName==elementName);
}

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

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

哪些值,是什么值?不清楚您想做什么。@JonSkeet,我想更改
标记内
标记下的值。您应该提供一个示例,说明更改后XML的外观。@DineshkumarPonnusamy:请将此编辑到您的问题中。我们不需要在或中阅读注释为了理解一个问题。@JonSkeet我在问题部分添加了。请帮助我解决这个问题。插槽值始终为null:-(.为什么如此?@DineshkumarPonnusamy可能是因为名称空间。你应该使用
名称。LocalName
而不是
名称
。我已经更新了我的答案
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");
public static IEnumerable<XElement> NsDescendants(this XContainer e, string elementName)
{
    return e.Descendants().Where(d => d.Name.LocalName == elementName);
}
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");