XmlReader,解析问题 [英] XmlReader, parsing problem

查看:103
本文介绍了XmlReader,解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在尝试使用XmlWriter创建一个XML文档然后再读回来。



创建文件的代码如下:

Hi all

I'm trying to create an XML document using XmlWriter and then read it back.

The code to create the file is below:

FileStream fs = new FileStream("g:\\" + id + ".xml", FileMode.Create);
XmlWriter sw = XmlWriter.Create(fs);             
sw.WriteStartElement("root");
sw.WriteElementString("Text", ucChoice.txtText.Text);
// iterate through choice rule in conditions
foreach (RuleInCondition ruleInCondition in ucChoice.RuleInConditions) {
           sw.WriteStartElement("rulein");
           sw.WriteElementString("truefalse", ruleInCondition.TrueFalse.ToString());
           sw.WriteElementString("specification", 
                    ruleInCondition.ThreeValueCriteria.Specification);
           sw.WriteEndElement();
            }             

sw.WriteEndElement();
sw.WriteEndDocument();
sw.Flush();





代码创建一个根,一个'text'元素,然后一个'rulein'带有一些子元素的元素提供有关'rulein'的信息



当我使用XmlReader读回它时,它无法识别'rulein'。



下面是回读的代码:



The code creates a root, a 'text' element, then a 'rulein' element with some subelements to give information about the 'rulein'

When I read it back using XmlReader, it isn't recognizing 'rulein'.

Below is the code to read it back:

XmlReader reader = XmlReader.Create("g:\\" + id + ".xml");
reader.MoveToContent(); 
while (reader.Read())  {
          if (reader.NodeType == XmlNodeType.Element)
          {
                if (reader.Name == "Text")
                {
                     XElement el = XNode.ReadFrom(reader) as XElement;
                     this.Text = el.Value;
                }

                else if (reader.Name == "rulein")
                {
                     RuleInCondition ruleInCondition = new RuleInCondition();
                     XElement el = XNode.ReadFrom(reader) as XElement;
                     foreach (XNode e in el.Nodes())
                        {
                            XElement child = e as XElement;
                            if (child.Name == "specification")
                                ruleInCondition.ThreeValueCriteria.Specification = child.Value;
                            else if (child.Name == "truefalse")
                                ruleInCondition.TrueFalse = Boolean.Parse(child.Value);
                 }





我不太了解XmlReader和XmlWriter。如何正确读取'rulein'?



I don't know XmlReader and XmlWriter very well. How do I get the 'rulein' read properly?

推荐答案

调试代码并在行上放置断点
Debug your code and put a breakpoint on the line
XElement el = XNode.ReadFrom(reader) as XElement;



当代码第一次到达该点时,将鼠标悬停在变量 reader 上你会注意到它的名字是文字 - 你会期待什么!

现在F10超过断点并再次悬停在读者上。 ..现在你看到读者的名字是rulein......换句话说,那行已经将xmlreader进一步移动到文件中...它不再指向你认为它的那条线......所以行


When the code reaches that point for the first time, hover over the variable reader and you will notice that it has the name "Text" - what you would expect!
Now F10 over the breakpoint and hover over reader again ... now you see that reader has the name "rulein" ... in other words that line has move the xmlreader further through the file ... it is no longer "pointing" at the line you think it is... so the line

else if (reader.Name == "rulein")

将直接跳过节点规则。

尝试删除 else 即使用

will skip straight over the node rulein.
Try removing the else i.e. use

if (reader.Name == "rulein")

...继续单步执行代码,您将看到它现在将落入 if 语句并处理rulein



注意,

... carry on stepping through the code and you will see that it will now drop into that if statement and handle "rulein"

Note, the same problem will arise with

else if (child.Name == "truefalse")

- 摆脱其他人。



看看这个例子 http:/ /www.dotnetperls.com/xmlreader [ ^ ]简单使用xmlreader 不用使用 XElement

- get rid of the else.

Have a look at this example http://www.dotnetperls.com/xmlreader[^] of a simple use of xmlreader without using XElement


一个简单的... C# XML Reader



华纳
A simple...C# XML Reader

Warner


这篇关于XmlReader,解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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