<CONTAINER>
     <SHORT-NAME>ESP_DiagExtModeSts</SHORT-NAME>
     <DEFINITION-REF DEST="PARAM-CONF-CONTAINER-DEF">/AUTOSAR/Com/ComConfig/ComSignal</DEFINITION-REF>
     <PARAMETER-VALUES>
      <BOOLEAN-VALUE>
       <DEFINITION-REF DEST="BOOLEAN-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComAliveCounter</DEFINITION-REF>
       <VALUE>false</VALUE>
      </BOOLEAN-VALUE>
      <BOOLEAN-VALUE>
       <DEFINITION-REF DEST="BOOLEAN-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComChecksum</DEFINITION-REF>
       <VALUE>false</VALUE>
      </BOOLEAN-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComBitPosition</DEFINITION-REF>
       <VALUE>3</VALUE>
      </INTEGER-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComSignalDataInvalidValue</DEFINITION-REF>
       <VALUE>0</VALUE>
      </INTEGER-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComSignalInitValue</DEFINITION-REF>
       <VALUE>0</VALUE>
      </INTEGER-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComSignalLength</DEFINITION-REF>
       <VALUE>1</VALUE>
      </INTEGER-VALUE>
      <ENUMERATION-VALUE>
       <DEFINITION-REF DEST="ENUMERATION-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComTransferProperty</DEFINITION-REF>
       <VALUE>PENDING</VALUE>
      </ENUMERATION-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComUpdateBitPosition</DEFINITION-REF>
       <VALUE>-1</VALUE>
      </INTEGER-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComBitSize</DEFINITION-REF>
       <VALUE>1</VALUE>
      </INTEGER-VALUE>
      <ENUMERATION-VALUE>
       <DEFINITION-REF DEST="ENUMERATION-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComDataInvalidAction</DEFINITION-REF>
       <VALUE>REPLACE</VALUE>
      </ENUMERATION-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComFirstTimeoutFactor</DEFINITION-REF>
       <VALUE>50</VALUE>
      </INTEGER-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComHandleId</DEFINITION-REF>
       <VALUE>67</VALUE>
      </INTEGER-VALUE>
      <FUNCTION-NAME-VALUE>
       <DEFINITION-REF DEST="FUNCTION-NAME-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComNotification</DEFINITION-REF>
       <VALUE>Rte_COMCbk_ESP_DiagExtModeSts</VALUE>
      </FUNCTION-NAME-VALUE>
      <ENUMERATION-VALUE>
       <DEFINITION-REF DEST="ENUMERATION-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComSignalEndianess</DEFINITION-REF>
       <VALUE>BIG_ENDIAN</VALUE>
      </ENUMERATION-VALUE>
      <ENUMERATION-VALUE>
       <DEFINITION-REF DEST="ENUMERATION-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComSignalType</DEFINITION-REF>
       <VALUE>UINT8</VALUE>
      </ENUMERATION-VALUE>
      <INTEGER-VALUE>
       <DEFINITION-REF DEST="INTEGER-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutFactor</DEFINITION-REF>
       <VALUE>20</VALUE>
      </INTEGER-VALUE>
      <FUNCTION-NAME-VALUE>
       <DEFINITION-REF DEST="FUNCTION-NAME-DEF">/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification</DEFINITION-REF>
       <VALUE>Rte_COMCbkTOut_ESP_DiagExtModeSts</VALUE>
      </FUNCTION-NAME-VALUE>
     </PARAMETER-VALUES>
     <REFERENCE-VALUES>
      <REFERENCE-VALUE>
       <DEFINITION-REF DEST="FOREIGN-REFERENCE-PARAM-DEF">/AUTOSAR/Com/ComConfig/ComSignal/SystemTemplateSystemSignalRef</DEFINITION-REF>
       <VALUE-REF DEST="I-SIGNAL-TO-I-PDU-MAPPING">/ComSignals/Pdus/msgIn_ESP_DA_Message/SG_sigESP_DiagExtSts</VALUE-REF>
      </REFERENCE-VALUE>
     </REFERENCE-VALUES>
     <SUB-CONTAINERS></SUB-CONTAINERS>
    </CONTAINER>

 private void button11_Click(object sender, EventArgs e)
    {
        XDocument doc = XDocument.Load(openFileDialog1.FileName);
        XNamespace ns = ((XElement)doc.FirstNode).Name.Namespace;

        List<XElement> containers = doc.Descendants(ns + "CONTAINER").ToList();
        foreach (XElement container in containers)
        {
            string shortName = (string)container.Element(ns + "SHORT-NAME");

            string def_ref = (string)container.Element(ns + "DEFINITION-REF").Value;

            if (shortName.Length > 0 && def_ref == "/AUTOSAR/Com/ComConfig/ComSignal")
            {
                List<XElement> def_ref1 = container.Descendants(ns + "FUNCTION-NAME-VALUE").Descendants(ns + "DEFINITION-REF").ToList();

                List<XElement> values = container.Descendants(ns + "FUNCTION-NAME-VALUE").Descendants(ns + "VALUE").ToList();

                foreach (XElement def in def_ref1)
                {
                    if ((string)def.Value == "/AUTOSAR/Com/ComConfig/ComSignal/ComNotification")
                    {
                        foreach (XElement value in values)
                        {
                                value.Value = "RTE_cbkTOut" + shortName;
                                listBox1.Items.Add(value.Value);
                        }
                    }
                }
            }
        }
        doc.Save(OpenFileDialog.FileName);
    }

这是XML文件的一部分,我的XML文件中有太多这样的。在这里,信号的名称是在tag中定义的,我想用带有前缀的信号名称来更改tag中存在的tag的内部文本。例如:rte_comcbk_signalname。
这必须在xml文件中存在的所有不同容器中完成。
请注意:所有容器的结构保持不变,只有标记中的内部文本因容器而异。
我能写出上述标记的前缀,我应该如何分配标记的信号名??
我该如何进一步?
 private void button11_Click(object sender, EventArgs e)
    {
    int count_ComTimeoutNotification = 0;
    XmlNodeList comTimeoutNotificationList = doc.GetElementsByTagName("DEFINITION-REF");
    int NodeListCount = comTimeoutNotificationList.Count;
    for (int i = 0; i < NodeListCount; i++)
        {


            if ((string)comTimeoutNotificationList[i].InnerText == "/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification")
            {

                if ((string)comTimeoutNotificationList[i].NextSibling.Name == "VALUE")
                {
                    comTimeoutNotificationList[i].NextSibling.InnerText = "_Shree";
                    count_ComTimeoutNotification++; //this line is for reference if its changing the exact no.of time that is reqd.
                }
            }
        }

文件保存(openfiledialog1.filename);
}

最佳答案

使用xml linq尝试类似的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication85
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = ((XElement)doc.FirstNode).Name.Namespace;

            List<XElement> containers = doc.Descendants(ns + "CONTAINER").ToList();
            foreach (XElement container in containers)
            {
                var definition_Ref = container.Descendants(ns + "FUNCTION-NAME-VALUE")
                    .Where(x => x.Descendants(ns + "DEFINITION-REF").Count() > 0).ToList();
                if (definition_Ref.Count > 0)
                {
                    string shortName = (string)container.Element(ns + "SHORT-NAME");
                    if (shortName.Length > 0)
                    {
                        List<XElement> functionName = definition_Ref
                            .Where(x => x.Value.Contains("/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification"))
                            .ToList();
                        XElement value = functionName.Elements("VALUE").FirstOrDefault();
                        if (value != null)
                        {
                            value.Value = shortName + "_" + value.Value;
                        }
                    }
                }
            }
        }
    }
}

关于c# - 更改嵌套节点的内部文本,并为其分配C#中父节点的第一个兄弟节点的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36643060/

10-10 16:15