简体   繁体   中英

How can i access the specific node i.e ServerUrl of the XML and update the innertext in c#?

    <?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>

I can access this xml from c# What i want to do is to access specific node of the xml and update the innertext of this node with the text which i enter from textbox which i have created. Nodename : inside node.

Please help me.

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

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM