如何更改Xml属性? [英] How Can I Change Xml Attribute?

查看:60
本文介绍了如何更改Xml属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在寻求建议。

我有一个XML文件,我的任务是更改节点的属性。但当我犯这个错误时:

需要命名空间管理器或XsltContext。

我的xml文件:

 <?  xml    版本  =  1.0   编码  =  UTF-8  >  
< soapenv:Envelope xmlns:soapenv = http://somelink.com >





我的代码:

 XmlDocument docreq =  new  XmlDocument(); 
docrev.Load( Mynew.xml);
XmlNode node = docreq.SelectSingleNode( soapenv:Envelope);
node.Attributes [ 0 ]。值= NEWLINK;
使用(TextWriter writer = new StreamWriter( Mynew.xml false ,Encoding.UTF8))
docreq .Save(作家);





如何在没有此错误的情况下将属性更改为NEWLINK?

解决方案

 XDocument xDocument = XDocument.Load(  MyXml.xml ); 

XElement soapenvXElement = xDocument.Descendants()。FirstOrDefault();

soapenvXElement.Value = NEWLINK;


Hi to all . I'm looking for advice .
I have an XML file , and my task is to change the attribute of node . But when i make this a have an error:
"Namespace Manager or XsltContext needed" .
My xml file :

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://somelink.com">



my code :

XmlDocument docreq = new XmlDocument();
    docrev.Load("Mynew.xml");
     XmlNode node = docreq.SelectSingleNode("soapenv:Envelope");
   node.Attributes[0].Value = "NEWLINK";
  using (TextWriter writer = new StreamWriter("Mynew.xml", false, Encoding.UTF8))
docreq.Save(writer);



How I can change the attribute to NEWLINK without this error?

解决方案

XDocument xDocument = XDocument.Load("MyXml.xml");

XElement soapenvXElement = xDocument.Descendants().FirstOrDefault();

soapenvXElement.Value = "NEWLINK";


这篇关于如何更改Xml属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆