C# 使用空格和引号作为属性修改xml节点

C# 使用空格和引号作为属性修改xml节点,c#,xml,xsd,nodes,C#,Xml,Xsd,Nodes,我试图访问XML的子节点,但我的第一个XML节点的属性是空格和引号 var xml = @"<Envelope xsd "http"> <Catalog> <Price> <Value Default ="yes">P1</Value> </Price> </Catalo

我试图访问XML的子节点,但我的第一个XML节点的属性是空格和引号

 var xml = @"<Envelope xsd "http">
            <Catalog>
                <Price>
                  <Value Default ="yes">P1</Value>
                </Price>
            </Catalog>
        </Envelope>";
有什么想法吗?

请参阅


使用//,而不是//,因为//获取文档的根

我认为这不是有效的xml,您可能是指以下内容吗

using System;
using System.Globalization;
using System.Xml;

namespace ConsoleApplication9
{
    class Program
    {
        private static void Main(string[] args)
        {
            //Valid XML
            string xml = @"<Envelope xsd='http'>
                          <Catalog>
                             <Price>
                               <Value Default='yes'>P1</Value>
                            </Price>
                          </Catalog>
                         </Envelope>";
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            //Select the Value Node
            XmlNode node = doc.SelectSingleNode("/*/Catalog/Price/Value");

            //Set the Default attribute to 1
            node.Attributes["Default"].Value = 1.ToString(CultureInfo.InvariantCulture);

            //Check the output
            Console.WriteLine(doc.InnerXml.ToString(CultureInfo.InvariantCulture));

            //Press enter to exit
            Console.ReadLine();
        }
    }
}

只是说说而已。

这可能看起来有点硬编码,但应该可以:

  XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    Namespace ns= "http"; //set the namespace of the root node here

    //the following is where you change the value to 1

 doc.Document.Descendants(ns+"Envelope").FirstorDefault().Descendants(ns+"Catalog").Descendants(ns+"Price").FirstorDefault().Elements("Value").Attribute("Default").SetValue("1");

另外,xml在我看来有点错误,正如有人提到的,根节点需要更正。

尝试过,但我想Stewart是对的。根从一开始就无效。我从xsd生成了xml。实际上,在这种情况下看起来是这样的,只要仔细检查一下,确保你有所有的选择。谢谢。将此标记为答案,因为它给出了正确的方向。
  XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    Namespace ns= "http"; //set the namespace of the root node here

    //the following is where you change the value to 1

 doc.Document.Descendants(ns+"Envelope").FirstorDefault().Descendants(ns+"Catalog").Descendants(ns+"Price").FirstorDefault().Elements("Value").Attribute("Default").SetValue("1");