开发者

C# .NET XMLWriter/Reader problem

I've been having problems writing XML and reading it in. I have a handwritten XML that gets read in fine, but after I write the XML it acts funny.

The output of the WriteXML: http://www.craigmouser.com/random/test.xml

It works if you hit enter after the (specials) tag. I.E. make (specials)(special) look like

(specials)

(special)

If I step through it, when reading it, it goes to the start node of specials, then the next iteration reads it as an EndElement with name Shots. I have no idea where to go from here. Thanks in advance.

Code: Writing

        public void SaveXMLFile(string filename, Bar b, Boolean saveOldData)
    {
        XmlWriter xml;
        if(filename.Contains(".xml"))
        {
            xml = XmlWriter.Create(filename);
        }
        else 
        {
            xml = XmlWriter.Create(filename + ".xml");
        }
        xml.WriteStartElement("AggievilleBar");
        xml.WriteElementString("name", b.Name);
        xml.WriteStartElement("picture");
        xml.WriteAttributeString("version", b.PictureVersion.ToString());
        xml.WriteEndElement();
        xml.WriteElementString("location", b.Location.Replace(Environment.NewLine, "\n"));
        xml.WriteElementString("news", b.News.Replace(Environment.NewLine, "\n"));
        xml.WriteElementString("description", b.Description.Replace(Environment.NewLine, "\n"));
        xml.WriteStartElement("specials");
        xml.WriteString("\n"); //This line fixes the problem... ?!?!
        foreach (Special s in b.Specials)
        {
            if (s.DayOfWeek > 0 || (s.DayOfWeek == -1 
                && ((s.Date.CompareTo(DateTime.Today) < 0 && saveOldData )
                || s.Date.CompareTo(DateTime.Today) >= 0)))
            {
                    xml.WriteStartElement("special");
                    xml.WriteAttributeString("dayofweek", s.DayOfWeek.ToString());
                    if (s.DayOfWeek == -1)
                        xml.WriteAttributeString("date", s.Date.ToString("yyyy-MM-dd"));
                    xml.WriteAttributeString("price", s.Price.ToString());
                    xml.WriteString(s.Name);
                    xml.WriteEndElement();
            }
        }
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.Close();
    }

Code: Reading

        public Bar LoadXMLFile(string filename)
    {
        List<Special> specials = new List<Special>();
        XmlReader xml;
        try
        {
            xml = XmlReader.Create(filename);
        }
        catch (Exception)
        {

            MessageBox.Show("Unable to open file. If you get this error upon opening the program, we failed to pull down your current data. You will most likely be unable to save, but you are free to try. If this problem persists please contact us at pulsarproductionssupport@gmail.com",
                "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
        }

        Bar current = new Bar();
        Special s = new Special();
        while (xml.Read())
        {
            if (xml.IsStartElement())
            {
                switch (xml.Name)
                {
                    case "AggievilleBar":
                        current = new Bar();
                        break;
                    case "name":
                        if (xml.Read())
                            current.Name = xml.Value.Trim();
                        break;
                    case "picture":
                        if (xml.HasAttributes)
                        {
                            try
                            {
                                current.PictureVersion = Int32.Parse(xml.GetAttribute("version"));
                            }
                            catch (Exception)
                            {

                                MessageBox.Show("Error reading in the Picture Version Number.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            }
                        } 
                        break;
                    case "location":
                        if (xml.Read())
                            current.Location = xml.Value.Trim();
                        break;
                    case "news":
                        if (xml.Read())
                            current.News = xml.Value.Trim();
                        break;
                    case "description":
                        if (xml.Read())
                            current.Description = xml.Value.Trim();
                        break;
                    case "specials":
                        if (xml.Read())
                            specials = new List<Special>();
                        break;
                    case "special":
                        s = new Special();
                        if (xml.HasAttributes)
                        {
                            try
                            {
                                s.DayOfWeek = Int32.Parse(xml.GetAttribute(0));
                                if (s.DayOfWeek == -1)
             开发者_如何学Python                   {
                                    s.Date = DateTime.Parse(xml.GetAttribute(1));
                                    s.Price = Int32.Parse(xml.GetAttribute(2));
                                }
                                else
                                    s.Price = Int32.Parse(xml.GetAttribute(1));
                            }
                            catch (Exception)
                            {

                                MessageBox.Show("Error reading in a special.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        if (xml.Read())
                            s.Name = xml.Value.Trim();
                        break;
                }
            }
            else
            {
                switch (xml.Name)
                {
                    case "AggievilleBar":
                        xml.Close();
                        break;
                    case "special":
                        specials.Add(s);
                        break;
                    case "specials":
                        current.Specials = specials;
                        break;
                }
            }
        }

        return current;
    }


Without seeing your code it's hard to really give a straight answer to that question. However, I can suggest using Linq-to-XML instead of XMLReader/XMLWriter -- it's so much easier to work with when you don't have to read each node one at a time and determine what node you're working with, which sounds like the problem you're having.

For example, code like:

using (var reader = new XmlReader(...))
{
    while reader.Read()
    {
        if (reader.Name = "book" && reader.IsStartElement)
        {
            // endless, confusing nesting!!!
        }
    }
}

Becomes:

var elem = doc.Descendants("book").Descendants("title")
           .Where(c => c.Attribute("name").Value == "C# Basics")
           .FirstOrDefault();

For an introduction to LINQ-to-XML, check out http://www.c-sharpcorner.com/UploadFile/shakthee/2868/, or just search for "Linq-to-XML". Plenty of examples out there.

Edit: I tried your code and I was able to reproduce your problem. It seems that without a newline before the special tag, the first special element is read in as IsStartElement() == false. I wasn't sure why this is; even skimmed through the XML Specifications and didn't see any requirements about newlines before elements.

I rewrote your code in Linq-to-XML and it worked fine without any newlines:

var xdoc = XDocument.Load(filename);
var barElement = xdoc.Element("AggievilleBar");
var specialElements = barElement.Descendants("special").ToList();
var specials = new List<Special>();
specialElements.ForEach(s =>
    {
        var dayOfWeek = Convert.ToInt32(s.Attribute("dayofweek").Value);
        var price = Convert.ToInt32(s.Attribute("price").Value);
        var date = s.Attribute("date");
        specials.Add(new Special
        {
            Name = s.Value,
            DayOfWeek = dayOfWeek,
            Price = price,
            Date = date != null ? DateTime.Parse(date.Value) : DateTime.MinValue
        });
    });
var bar = new Bar() {
    Name = barElement.Element("name").Value,
    PictureVersion = Convert.ToInt32(barElement.Elements("picture").Single()
        .Attribute("version").Value),
    Location = barElement.Element("location").Value,
    Description = barElement.Element("description").Value,
    News = barElement.Element("news").Value,
    Specials = specials
};
return bar;

Would you consider using Linq-to-XML instead of XMLReader? I've had my share of trouble with XMLReader in the past and once I switched to Linq-to-XML haven't looked back!

EDIT: I know this question is rather old now, but I just came across an article that reminded me of this question and might explain why this is happening: --> http://www.codeproject.com/KB/dotnet/pitfalls_xml_4_0.aspx

The author states:

In this light, a nasty difference between XmlReaders/Writers and XDocument is the way whitespace is treated. (See http://msdn.microsoft.com/en-us/library/bb387014.aspx.)

From msdn:

In most cases, if the method takes LoadOptions as an argument, you can optionally preserve insignificant white space as text nodes in the XML tree. However, if the method is loading the XML from an XmlReader, then the XmlReader determines whether white space will be preserved or not. Setting PreserveWhitespace will have no effect.

So perhaps, since you're loading using an XmlReader, the XmlReader is making the determination as to whether or not it should preserve white space. Most likely it IS preserving the white space which is why the newline (or lack thereof) makes a difference. And it doesn't seem like you can do anything to change it, so long as you're using an XmlReader! Very peculiar.


I'd recommend you use the XmlDocument class and its Load and Save methods, and then work with the XML tree instead of messing around with XmlReader and XmlWriter. In my experience using XmlDocument has fewer weird formatting problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜