Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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#XmlTextReader未读取所有元素和属性_C#_Xml_Winforms - Fatal编程技术网

C#XmlTextReader未读取所有元素和属性

C#XmlTextReader未读取所有元素和属性,c#,xml,winforms,C#,Xml,Winforms,我试图从xml文件中读取数据并将其显示在文本框中,但它只显示最后一个元素/属性,在本例中为“持久性”。这是我的xml文件 <?xml version="1.0" encoding="utf-8"?> <Character> <Name Name="Test" /> <Age Age="19" /> <Class Class="Necromancer" /> <Strength Strength

我试图从xml文件中读取数据并将其显示在文本框中,但它只显示最后一个元素/属性,在本例中为“持久性”。这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<Character>
  <Name
   Name="Test" />
   <Age
   Age="19" />
  <Class
  Class="Necromancer" />
  <Strength
  Strength="1" />
  <Dexterity
  Dexterity="2" />
  <Intelligence
  Intelligence="3" />
  <Speed
  Speed="4" />
  <Endurance
  Endurance="5" />
</Character>

我给读者的代码如下

XmlTextReader reader = new XmlTextReader(openFileDialog1.FileName);
while (reader.Read())
{
   if (reader.HasAttributes)
   {
     for (int i = 0; i < reader.AttributeCount; i++)
     {
       reader.MoveToAttribute(i);
       switch (reader.Name)
       {
         case "Name":
             DisplayBox.Text = "Name: " + reader.Value + "\n";
             break;
         case "Age":
             DisplayBox.Text = "Age: " + reader.Value + "\n";
             break;
         case "Class":
             DisplayBox.Text = "Class: " + reader.Value + "\n";
             break;
         case "Strength":
             DisplayBox.Text = "Strength: " + reader.Value + "\n";
             break;
         case "Dexterity":
             DisplayBox.Text = "Dexterity: " + reader.Value + "\n";
             break;
         case "Intelligence":
             DisplayBox.Text = "Intelligence: " + reader.Value + "\n";
             break;
         case "Speed":
             DisplayBox.Text = "Speed: " + reader.Value + "\n";
             break;
         case "Endurance":
             DisplayBox.Text = "Endurance: " + reader.Value + "\n";
             break;
         default:
             break;
       }
     }
         reader.MoveToElement();
  }
}
XmlTextReader=newxmltextreader(openFileDialog1.FileName);
while(reader.Read())
{
if(reader.HasAttributes)
{
for(int i=0;i
因此,每当我单击按钮显示数据时,文本框中唯一显示的是耐久性:5而不是

    DisplayBox.Text = 
你应该用like

   DisplayBox.Text += 

它可以满足要求。

看起来您需要更换

DisplayBox.Text =

此外,所有的开关条件都非常相似。因此,您可以使用以下选项:

string[] supportedAttributes = new []{"Name", "Age", "Class", "Strength", "Dexterity", "Intelligence", "Speed", "Endurance"};
while (reader.Read())
{
   if (reader.HasAttributes)
   {
     for (int i = 0; i < reader.AttributeCount; i++)
     {
       reader.MoveToAttribute(i);
       if(supportedAttributes.Any(a=>a == reader.Name))
           DisplayBox.Text += string.Format("{0}: {1} \n", reader.Name, reader.Value);
     }
     reader.MoveToElement();
  }
}
string[]supportedAttribute=new[]{“姓名”、“年龄”、“职业”、“力量”、“灵巧”、“智力”、“速度”、“耐力”};
while(reader.Read())
{
if(reader.HasAttributes)
{
for(int i=0;ia==reader.Name))
DisplayBox.Text+=string.Format(“{0}:{1}\n”,reader.Name,reader.Value);
}
reader.MoveToElement();
}
}

当前,您正在所有节点之间循环

最后,循环在最后一个节点停止。那只不过是耐力节点

所以你会看到结果是耐力

你需要检查具体的条件,如果它满足,那么你需要出来的循环


如果您想显示文本框中的所有值,请按照@Kostya@Furqan回答。

我没有直接回答您的问题,而是提供了另一种编写代码的方法

尤其是
开关
语句本身会重复,并且可以删除重复

还可以使用
开关
语句将代码锁定到特定值。您不能动态更改要使用的属性名称列表,例如,可能是针对不同的语言

这是我的密码:

var xd = XDocument.Load(openFileDialog1.FileName);

var query =
    from xe in xd.Descendants()
    from xa in xe.Attributes()
    let r = map(xa.Name.ToString(), xa.Value)
    where r != null
    select r;

DisplayBox.Text = String.Join("", query);
现在唯一缺少的是
map
函数的定义。这就是代码变得有点急躁的地方

首先从您要查找的名称开始:

var names = new []
{
    "Name", "Age", "Class",
    "Strength", "Dexterity", "Intelligence",
    "Speed", "Endurance", 
};
现在我们只需要定义几个负责映射的变量:

var nameMap =
    names
        .ToDictionary(
            n => n,
            n => (Func<string, string>)
                (t => String.Format("{0}: {1}\n", n, t)));

Func<string, string, string> map =
    (n, v) =>
        nameMap.ContainsKey(n) ? nameMap[n](v) : null;
var-nameMap=
名字
.ToDictionary(
n=>n,
n=>(Func)
(t=>String.Format(“{0}:{1}\n”,n,t));
Func映射=
(n,v)=>
姓名地图。集装箱(n)?nameMap[n](v):空;
这有点棘手,但它很好地将名称列表从最终查询中分离出来,以获取数据,并将使代码更清晰地显示需要维护的部分

var nameMap =
    names
        .ToDictionary(
            n => n,
            n => (Func<string, string>)
                (t => String.Format("{0}: {1}\n", n, t)));

Func<string, string, string> map =
    (n, v) =>
        nameMap.ContainsKey(n) ? nameMap[n](v) : null;