Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 筛选XML元素并编辑其值,ASP.NET_C#_Asp.net_Xml - Fatal编程技术网

C# 筛选XML元素并编辑其值,ASP.NET

C# 筛选XML元素并编辑其值,ASP.NET,c#,asp.net,xml,C#,Asp.net,Xml,我想根据id xmlelement值过滤我的代码,通过查询字符串过滤,但它似乎不起作用 XmlDocument xdoc = new XmlDocument(); xdoc.Load(filepath); XmlNode root = xdoc.DocumentElement; XmlNode idNode = root.SelectSingleNode("/students/student/id"); if (idNode.Value == null){create

我想根据id xmlelement值过滤我的代码,通过查询字符串过滤,但它似乎不起作用

XmlDocument xdoc = new XmlDocument();
xdoc.Load(filepath);

XmlNode root = xdoc.DocumentElement;
XmlNode idNode = root.SelectSingleNode("/students/student/id");

           if (idNode.Value == null){create a new xml node}
else if (idNode.Value != null) {update the xml element with the value of id)
我试图解释它的问题,这里是链接


如果我理解正确,您希望添加student元素,前提是没有指定id的student(并且可能向student节点添加一些数据)。以下是Linq到xml的解决方案:

int id = 2;
XDocument xdoc = XDocument.Load(filepath);
XElement student = xdoc.Descendants("student")
        .Where(s => (int)s.Element("id") == id)
        .SingleOrDefault();

if (student == null)
{
    student = new XElement("student",
                        new XElement("id", id),
                        new XElement("first_name"),
                        new XElement("last_name")); // add other elements here
    xdoc.Root.Add(student);
}

student.Element("first_name").Value = TextBox_firstname.Text;
student.Element("last_name").Value = TextBox_lastname.Text;
// set other values here          

xdoc.Save(filepath);

定义不工作。您面临的具体错误是什么?请将您的问题分成几个单独的问题,并解释您无法实现的问题。例如,“如何根据'id'元素的值选择Xml节点”,“如何创建Xml节点”,。。。(您之前的一个可能没有得到回复,因为其中包含大量代码…)当我想要创建一个新的xml节点时,,,它没有创建一个新的xml节点…它也没有更新xml节点…您真的需要使用
XmlDocument
而不是LINQ to xml吗?后者更好。我希望能够基于现有XML节点的id(XML元素)值编辑它们的值,以及..@user1750680请参阅更新。如果xml中不存在student,则使用提供的id和所有其他空元素创建student。稍后您将更新student(不管您是通过id找到的,还是在上一步中添加的)1 ahmad hani这是我的xml,,,我如何才能获得value id元素,以便能够将其放入您在上面创建的“student”元素中…@user1750680 id element added here
new XElement(“id”,id)
-它创建了一个名为
且id值为的新元素,用于在页面中搜索studentin\u Load i将查询字符串值分配给TextBox、、、student.element(“first\u name”)。value=TextBox\u firstname.Text;不保存用户编辑的数据(来自文本框),它会在另一个节点中重新保存查询字符串的值。。。我怎样才能解决这个问题。。?