Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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数据时为Null值_C#_Asp.net_Xml_Web - Fatal编程技术网

C# 尝试读取XML数据时为Null值

C# 尝试读取XML数据时为Null值,c#,asp.net,xml,web,C#,Asp.net,Xml,Web,我尝试读取与我的post id字符串(非null)属性匹配的XML数据,但它返回null,尽管我已经尝试过多次 protected void Update_btn_click(object sender, EventArgs e) { String new_title = newtitle.Text.ToString(); String new_description = update_des.Value.ToString(); String postid = Reque

我尝试读取与我的post id字符串(非null)属性匹配的XML数据,但它返回null,尽管我已经尝试过多次

protected void Update_btn_click(object sender, EventArgs e)
{
    String new_title = newtitle.Text.ToString();
    String new_description = update_des.Value.ToString();
    String postid = Request.QueryString["pid"];

    System.Diagnostics.Debug.WriteLine(postid);

    string filename = "C:\\Users\\user\\Source\\Repos\\FoodBlog\\FoodBlog\\Data\\blog_post.xml";
    XmlDocument xml_doc = new XmlDocument();
    xml_doc.Load(filename);

    XmlTextReader xtr = new XmlTextReader(filename);

    XmlNodeList elemList = xml_doc.GetElementsByTagName("post");
    for (int i = 0; i < elemList.Count; i++)
    {
        string getValue = elemList[i].Attributes[postid].ChildNodes[1].InnerText;
        System.Diagnostics.Debug.WriteLine(getValue);
    }
}
受保护的无效更新\u btn\u单击(对象发送方,事件参数e)
{
字符串new_title=newtitle.Text.ToString();
字符串new_description=update_des.Value.ToString();
字符串postid=Request.QueryString[“pid”];
系统.诊断.调试.写入线(postid);
string filename=“C:\\Users\\user\\Source\\Repos\\FoodBlog\\FoodBlog\\Data\\blog\u post.xml”;
XmlDocument xml_doc=新的XmlDocument();
xml_doc.Load(文件名);
XmlTextReader xtr=新的XmlTextReader(文件名);
XmlNodeList elemList=xml_doc.GetElementsByTagName(“post”);
for(int i=0;i
XML数据:

<Posts>
  <post pid="pid2623">
    <title>Test</title>
    <description>Test</description>
    <subtitle>Test</subtitle>
    <date>7/29/2018 12:00:00 AM</date>
    <author>est</author>
  </post>
</Posts>

试验
试验
试验
2018年7月29日上午12:00:00

我认为你的代码有问题

您需要修改这些行,因为在for循环中,您正在查找包含名为pid2623的属性的元素,但实际上您需要查找其pid属性包含值pid2623

XmlNodeList elemList = xml_doc.GetElementsByTagName("post");
for (int i=0;i<elemList.Count; i ++)
{
    string getValue = elemList[i].Attributes[postid].ChildNodes[1].InnerText;
    System.Diagnostics.Debug.WriteLine(getValue);
}

使用Linq to SQL IMHO更容易:

XDocument xml_doc = XDocument.Load(filename);

foreach (var p in xml_doc.Elements("Posts"))
{
    System.Diagnostics.Debug
         .WriteLine((string)p.Element("post").Attribute("pid"));
}
您可以查询如下特定帖子:

var post = from p in xml_doc.Elements("Posts")
           where (string)p.Element("post").Attribute("pid") == postid
           select p.Element("post");

什么是
null
?有例外吗?@EdSF嗨,先生,出于某种原因,它不会返回任何值。我将上传一张图片来解释错误。您好,先生,我已经实现了您的代码,并决定检索title的值,但仍然没有得到任何值,不知道为什么?对不起,我真的很新(您的代码)XmlNode elemList=xml_doc。选择singlenode(“/Posts/Post[@pid=“+postid+”)”;(我的代码)字符串测试=elemList.ChildNodes[0]。InnerText;系统.诊断.调试.写线(测试);很抱歉,您可以写一行代码来检索elemList变量中的所有xml元素吗。还有一件事,我的代码中有一个问题,我忘了添加一个引号,如果您准确地复制我的代码并将其粘贴到编辑器中,它现在应该可以工作了
var post = from p in xml_doc.Elements("Posts")
           where (string)p.Element("post").Attribute("pid") == postid
           select p.Element("post");