没有来自xml C#的子节点的结果 [英] No result of child nodes from xml C#

查看:74
本文介绍了没有来自xml C#的子节点的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml,我试图抓住每个以名称标题开头的子节点,但只在名为system的父节点下,并且属性等于值28。因此,如果程序将标题视为父节点系统下的子节点,但系统没有等于28的属性,那么它将省略该子节点。





 public void node()
{
XmlTextReader reader = new XmlTextReader(@C:\\\ForFun\Node.xml );


while(reader.Read())
{

if(reader.Name ==system)
{


if(!(null == reader.GetAttribute(28)))

{
XmlReader inner = reader.ReadSubtree();
List< string> data = new List< string>();

while(inner.Read())
{
if(reader.Name ==title)
{
data.Add(reader .ReadString());
}
}
Console.WriteLine(data);
}

}

}
}





这是我运行程序时的代码没有显示错误消息但是我只是得到一个空白的控制台屏幕。感谢任何提示或建议



这是一个样本xml



 -  <  信息类型    infotypevalue   =  44    id   =  id4049  >  
< infotypename > 删除和安装< / infotypename >
- < servinfo id = GUID-5FCEADFD-AFFA-4E01-B2C5-BA88E8325410 >
< title > Baggae Claim < / title >
< ptxt > GUID-5FCEADFD-AFFA-4E01-B2C5-BA88E8325410.xml < / ptxt >
< < span class =code-leadattribute> / servinfo >
< / infotype >
< / component >
< / subsystem >
< / system >
- < system catnbr = 28 id = id4051 >
< system-toc groupname = 28 / >
< systemname > 28 - 值代码< / systemname >
- < 组件 compid = 00088 id = id4052 >
< componentname < span class =code-keyword>> CLUSTER,Instrument < / componentname >
- < infotype infotypevalue = 03 id = id4053 >
< infotypename > 测试人员类< / infotypename >
- < servinfo id = GUID-F3F3BDA2-0DD4-4DD0-9E03-132CC780A889 >
< title > B1000-44-电子控制单元 - 数据存储器故障< span class =code-keyword>< / title >
< ptxt > GUID- F3F3BDA2-0DD4-4DD0-9E03-132CC780A889.xml < / ptxt >
< / servinfo >
- <
servinfo id = GUID-6767D65E-FD48-465C-A304-6F4690C9D174 >
< title > ; B1000-45-电子控制单元 - 程序存储器找到< / title >
< ptxt < span class =code-keyword>> GUID-6767D65E-FD48-465C-A304-6F4690C9D174.xml < / ptxt >
< / servinfo &g t;





以下是一个示例输出:

只有具有属性的系统下的节点值28被选中



 CLUSTER,Instrument 



 B1000-44-电子控制单元 - 数据存储器故障



 GUID-F3F3BDA2-0DD4-4DD0-9E03-132CC780A889 



 B1000-45-电子控制单元 - 程序存储器找到



 GUID- 6767D65E-FD48-465C-A304-6F4690C9D174 

解决方案

您不能使用 == 比较C#中的字符串,因为它并不总是按预期运行。

严格来说,如果两个字符串的类型都是 string == 有效。

在你的程序中,一个部分来自一个对象,确切的类型可能不一样。



http: //stackoverflow.com/questio ns / 1659097 / why-you-you-use-string-equals-over [ ^ ]

http://csharp.net-informations.com/string/csharp-string-compare.htm [ ^ ]

http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp [ ^ ]

http://stackoverflow.com /问题/ 3678792 /是字符串,等于和运营商,really-相同 [ ^ ]




建议:使用调试器逐步查看代码,跟踪变量。

调试器将允许您查看正在执行代码的内容以及不按预期运行的位置/时间。



PS:看起来我有点太快了在回答。


 reader.GetAttribute(  28

没有得到值为28的属性,我对你没有输出感到惊讶。它获取名称为28的属性,这不是有效的属性名称。

所以首先找到正确的元素类型。

然后得到正确名称的属性。

然后检查它的值是否为28.



inner.Read( )循环应该使用 inner 而不是 reader

  while (inner.Read())
{
if (inner.Name == title
{
data.Add(inner.ReadString());
}
}



最后写一个 List< string> 的所有字符串到控制台,尝试:

 data.ForEach(Console.WriteLine); 


尝试 Console.WriteLine(data.length +);



-KR

I have an xml in which I am trying to grab every child node that start with the name title but only under a parent node named system and has an attribute equal to the value "28". So if the program sees title as a child node under the parent node system but system doesn't have an attribute that equals 28 then it will omit that child node.


public void node()
      {
          XmlTextReader reader = new XmlTextReader(@"C:\pg\ForFun\Node.xml");


          while (reader.Read())
          {

              if (reader.Name == "system")
              {


                  if (!(null == reader.GetAttribute("28")))

                  {
                      XmlReader inner = reader.ReadSubtree();
                      List<string> data = new List<string>();

                      while (inner.Read())
                      {
                          if (reader.Name == "title")
                          {
                              data.Add(reader.ReadString());
                          }
                      }
                      Console.WriteLine(data);
                  }

              }

          }
      }



This is my code when I run the program no error messages shows up however I just get a blank console screen. Appreciate any tips or suggestions

Here is a sample xml

- <infotype infotypevalue="44" id="id4049">
  <infotypename>Removal and Installation</infotypename>
- <servinfo id="GUID-5FCEADFD-AFFA-4E01-B2C5-BA88E8325410">
  <title> Baggae Claims</title>
  <ptxt>GUID-5FCEADFD-AFFA-4E01-B2C5-BA88E8325410.xml</ptxt>
  </servinfo>
  </infotype>
  </component>
  </subsystem>
  </system>
- <system catnbr="28" id="id4051">
  <system-toc groupname="28" />
  <systemname>28 - Value Codes</systemname>
- <component compid="00088" id="id4052">
  <componentname>CLUSTER, Instrument</componentname>
- <infotype infotypevalue="03" id="id4053">
  <infotypename>Tester Class</infotypename>
- <servinfo id="GUID-F3F3BDA2-0DD4-4DD0-9E03-132CC780A889">
  <title>B1000-44-ELECTRONIC CONTROL UNIT - DATA MEMORY FAILURE</title>
  <ptxt>GUID-F3F3BDA2-0DD4-4DD0-9E03-132CC780A889.xml</ptxt>
  </servinfo>
- <servinfo id="GUID-6767D65E-FD48-465C-A304-6F4690C9D174">
  <title>B1000-45-ELECTRONIC CONTROL UNIT - PROGRAM MEMORY FOUND</title>
  <ptxt>GUID-6767D65E-FD48-465C-A304-6F4690C9D174.xml</ptxt>
  </servinfo>



Here is a sample output:
Only nodes under system that has attribute value "28" is selected

CLUSTER, Instrument


B1000-44-ELECTRONIC CONTROL UNIT - DATA MEMORY FAILURE


GUID-F3F3BDA2-0DD4-4DD0-9E03-132CC780A889


B1000-45-ELECTRONIC CONTROL UNIT - PROGRAM MEMORY FOUND


GUID-6767D65E-FD48-465C-A304-6F4690C9D174

解决方案

You can't use == to compare strings in C#, because it don't always behave as you expect.
Strictly speaking, if both strings are of type string, == works.
In your program, one part comes from an object and the exact type can be not the same.

http://stackoverflow.com/questions/1659097/why-would-you-use-string-equals-over[^]
http://csharp.net-informations.com/string/csharp-string-compare.htm[^]
http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp[^]
http://stackoverflow.com/questions/3678792/are-string-equals-and-operator-really-same[^]


Advice: use the debugger to see your code execute step by step, track variables.
The debugger will allow you to see what is doing your code and where/when it don't behave as expected.

PS: Looks like I was a little too fast at answering.


Since

reader.GetAttribute("28")

doesn't get the attribute with a value of "28", I'm not surprised you get no output. It gets the attribute with the name of "28", and that wouldn't be a valid attribute name.
So find the correct type of element first.
Then get the attribute by the correct name.
Then check if its value is 28.

The inner.Read() loop should be using inner instead of reader:

while (inner.Read())
{
  if (inner.Name == "title")
  {
    data.Add(inner.ReadString());
  }
}


Finally to write all the strings of a List<string> to the Console, try:

data.ForEach(Console.WriteLine);


Try with Console.WriteLine(data.length + "");

-KR


这篇关于没有来自xml C#的子节点的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆