Visual Studio 如何更改XML元素值

h5qlskok  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(136)

我需要你的帮助来完成我的任务也从中获得一些知识。
我有如下的XMl

<?xml version="1.0" encoding="UTF-8"?>
<spml:batchRequest xmlns:subscriber="urn:abc:names:prov:gw:SUBSCRIBER:3:0" xmlns:spml="urn:abc:names:prov:gw:SPML:2:0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" execution="synchronous" timestamp="true" processing="sequential" onError="exit_commit">
    <version>SUBSCRIBER_v30</version>
    <request xsi:type="spml:ModifyRequest" returnResultingObject="full">
        <version>SUBSCRIBER_v30</version>
        <objectclass>Subscriber</objectclass>
        <identifier>1234567890</identifier>
        <modification name="udm5gData/servingPlmnId[@plmnId='20201']/provisionedData/sessionManagementSubscriptionData[@singleNssai='1-000002']/dnnConfiguration[@dnnId='dj.nmdd']" operation="remove" scope="uniqueTypeMapping" xmlns:subscriber="urn:abc:names:prov:gw:SUBSCRIBER:3:0">
        </modification>
    </request>
</spml:batchRequest>

我需要从修改标记中修改@singleNssai ='1-000002',有人能帮我修改吗?
类似于@singleNssai ='2-000001'的内容
我正在尝试像下面这样

XmlDataDocument doc = new XmlDataDocument();
               doc.Load("D:\\soaptemp\\test.xml");

               XmlNode singlenssais = 
               doc.SelectSingleNode("//modification/name/@singleNssai");
                if (singlenssais != null)
                {
                    singlenssais.Value = "2-000001"; // Set to new value.
                }
gcxthw6b

gcxthw6b1#

使用Xml Linq

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

namespace ConsoleApplication40
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement modification = doc.Descendants("modification").First();
            XAttribute name = modification.Attribute("name");
            string strName = (string)name;
            strName = strName.Replace("1-000002", "2-000001");
            name.SetValue(strName);

        }  
    }
 
}

相关问题