【问题标题】:Xml Parsing in Visual Studio 2015Visual Studio 2015 中的 Xml 解析
【发布时间】:2017-04-28 23:49:25
【问题描述】:

我的 xml:

  <Absolventi nume="Ace Timisoara">
    <id>7</id>
    <oras>Timisoara</oras>
  </Absolventi>

我的代码是这样的:

        foreach (XmlNode node in doc.DocumentElement)
        {

            string nume = node.Attributes["nume"].Value;
            string oras = node.Attributes[0].InnerText;
            int id = int.Parse(node["id"].InnerText);

如果我运行它,我会看到: https://gyazo.com/e9a213267ee42bd2f671c4325c4b746d

在属性网格中,oras不应该和nume相同

        string nume = node.Attributes[0].InnerText;
        **string oras = node.Attributes[0].InnerText;**
        int id = int.Parse(node["id"].InnerText);

我不知道在节点 oras 上写什么以显示它的价值。我已经尝试了几十种东西,而不是属性、价值等,但它要么不起作用,要么只是崩溃。 我尝试将值放入 [name] ["name"] [string name] 等方括号中,但它们都没有工作,或者导致应用程序崩溃..

提前谢谢你

【问题讨论】:

  • 不显示代码图片。粘贴并格式化
  • 完成了,你能给我一个关于我的问题的提示吗?
  • 查看string oras = 的代码。正如您所注意到的,这是不正确的。尝试找出原因。也许开始尝试回答为什么实际上从 XML 中正确检索了“nume”和“id”。 "nume" 和 "id" 的 C# 代码如何与 XML 匹配,以及 "oras" not 的 C# 代码如何与 XML 匹配。请记住,您编写的 C# 代码不仅仅是一堆中间有几个点和分号的随机单词。这些在您上面的代码 sn-p 中显示为蓝色的单词具有非常具体的含义。阅读每个文档(在 VS 中,将光标移到它们上并按 F1)...
  • 您确实意识到这个问题甚至与 Visual Studio 无关,对吧?
  • 为什么?它是在 VS , c# 中完成的......甚至有人解决了我的问题

标签: c# xml parsing


【解决方案1】:

你需要像这样检查 oras

string doc = @"<Absolventi nume='Ace Timisoara'>
<id>7</id>
<oras>Timisoara</oras>
</Absolventi>";
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(doc);
foreach (XmlNode node in xDoc.ChildNodes)
{
    Console.WriteLine(node.Attributes["nume"].Value);
    Console.WriteLine(node["id"].InnerText);
    Console.WriteLine(node["oras"].InnerText);   
}

Test here

【讨论】:

  • 非常感谢,它成功了,那些(近节点真的成功了!!!!
猜你喜欢
  • 1970-01-01
  • 2015-10-30
  • 2015-11-09
  • 2017-06-20
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多