Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.40/5 (2 votes)
See more:
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:
C#
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();


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:
C#
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);
                 }


I don't know XmlReader and XmlWriter very well. How do I get the 'rulein' read properly?
Posted
Updated 6-Apr-14 4:09am
v2

Debug your code and put a breakpoint on the line
XElement el = XNode.ReadFrom(reader) as XElement;

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
C#
else if (reader.Name == "rulein")
will skip straight over the node rulein.
Try removing the else i.e. use
C#
if (reader.Name == "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
C#
else if (child.Name == "truefalse")
- 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
 
Share this answer
 
A simple...C# XML Reader

Warner
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900