如何在C#/XML中用新元素更新旧元素

如何在C#/XML中用新元素更新旧元素,c#,.net,xml,wpf,xaml,C#,.net,Xml,Wpf,Xaml,基本上,我想做的是:下载一个新的XML文件,并用旧文件替换一些元素,例如替换以下代码: <Run x:Name="Degree" Text="15"/> 使用xml linq: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplicati

基本上,我想做的是:下载一个新的XML文件,并用旧文件替换一些元素,例如替换以下代码:

<Run x:Name="Degree" Text="15"/>
使用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 URL = "https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(URL);

            XElement temperature = doc.Descendants("temperature").FirstOrDefault();
            temperature.SetAttributeValue("value", 281);

            string oldXml = "<Root xmlns:x=\"abc\"><Run x:Name=\"Degree\" Text=\"15\"/></Root>";

            XDocument oldDoc = XDocument.Parse(oldXml);
            XElement run = oldDoc.Descendants("Run").FirstOrDefault();

            run.ReplaceWith(temperature);

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串URL=”https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(URL);
XElement temperature=doc.subjects(“温度”).FirstOrDefault();
设置属性值(“值”,281);
字符串oldXml=“”;
XDocument oldDoc=XDocument.Parse(oldXml);
XElement run=oldDoc.substands(“run”).FirstOrDefault();
运行。替换为(温度);
}
}
}

您需要将数据绑定到WPF窗口。首先将xml转换为某种对象形式,然后将对象作为DataContext绑定到窗口。还有更简单的方法,在代码隐藏中使用控件名,并将值绑定到Run或TextBlock元素。
x:name
表示原始“代码”是XAML。但您的意思是希望用XML替换它。确切地说,你认为这会起什么作用?遵循正常的MVVM实践并将数据(XML)与视图(XAML)完全隔离不是更好吗?充其量,你的问题太宽泛了,坦率地说,你甚至不清楚你在问什么。我如何将我的代码中的
值设置为
温度
一?
using (WebClient web = new WebClient())
{
    string url = string.Format("https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22");
    var xml = web.DownloadString(url);
}
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 URL = "https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(URL);

            XElement temperature = doc.Descendants("temperature").FirstOrDefault();
            temperature.SetAttributeValue("value", 281);

            string oldXml = "<Root xmlns:x=\"abc\"><Run x:Name=\"Degree\" Text=\"15\"/></Root>";

            XDocument oldDoc = XDocument.Parse(oldXml);
            XElement run = oldDoc.Descendants("Run").FirstOrDefault();

            run.ReplaceWith(temperature);

        }
    }
}