如何访问特定节点,即XML的ServerUrl并更新c#中的innertext?

时间:2018-03-07 06:18:46

标签: c# xml

    <?xml version="1.0" encoding="utf-8"?>
<ApplicationConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ua="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/SDK/Configuration.xsd">

  <Extensions>
    <ua:XmlElement>
      <ComWrapperServerConfiguration xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://opcfoundation.org/UA/SDK/COMInterop">
        <WrappedServers>
          <ComClientConfiguration i:type="ComDaClientConfiguration">
            <ServerUrl>opc.com://localhost/Softing.OPCToolboxDemo_ServerDA.1</ServerUrl>
            <ServerName>DA</ServerName>
            <MaxReconnectWait>10000</MaxReconnectWait>
            <SeperatorChars></SeperatorChars>
            <AvailableLocales>
              <ua:String>en-US</ua:String>
              <ua:String>de-DE</ua:String>
              <ua:String>ja-JP</ua:String>
            </AvailableLocales>
            <BrowseToNotSupported>false</BrowseToNotSupported>
          </ComClientConfiguration>
          <ComClientConfiguration i:type="ComAeClientConfiguration">
            <ServerUrl>opc.com://localhost/Softing.OPCToolboxDemo_ServerAE.1/{2E565243-B238-11D3-842D-0008C779D775}</ServerUrl>
            <ServerName>AE</ServerName>
            <MaxReconnectWait>10000000</MaxReconnectWait>
            <SeperatorChars>\</SeperatorChars>
            <AvailableLocales>
              <ua:String>en-US</ua:String>
              <ua:String>de-DE</ua:String>
              <ua:String>ja-JP</ua:String>
            </AvailableLocales>
          </ComClientConfiguration>
          <!--
          <ComClientConfiguration i:type="ComHdaClientConfiguration">
            <ServerUrl>opc.com://localhost/OPCSample.OpcHdaServer/{6a5eedec-1509-4627-997f-993ccb65ab7c}</ServerUrl>
            <ServerName>HDA</ServerName>
            <MaxReconnectWait>10000</MaxReconnectWait>
            <SeperatorChars></SeperatorChars>
            <AddCapabilitiesToServerObject>true</AddCapabilitiesToServerObject>
            <AttributeSamplingInterval>1000</AttributeSamplingInterval>
            <TreatUncertainAsBad>true</TreatUncertainAsBad>
            <PercentDataBad>0</PercentDataBad>
            <PercentDataGood>100</PercentDataGood>
            <SteppedSlopedExtrapolation>false</SteppedSlopedExtrapolation>
          </ComClientConfiguration>
          -->
        </WrappedServers>
      </ComWrapperServerConfiguration>
    </ua:XmlElement>
  </Extensions>

我可以从c#访问这个xml 我想要做的是访问xml的特定节点并使用我从我创建的文本框输入的文本更新此节点的innertext。 Nodename:内部节点。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用xml linq:

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


namespace ConsoleApplication1
{
    class Program
    {
        const String FILENAME  = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            List<XElement> comClientConfigurations = doc.Descendants().Where(x => x.Name.LocalName == "ComClientConfiguration").ToList();
            XNamespace ns = comClientConfigurations[0].GetDefaultNamespace();



            XElement comClientConfiguration = comClientConfigurations.Where(x => (string)x.Element(ns + "ServerName") == "DA").FirstOrDefault();

            comClientConfiguration.SetElementValue(ns + "MaxReconnectWait", 12345);

        }
    }
}