What's so bad about building XML with string concatenation?

前端 未结 12 1360
长发绾君心
长发绾君心 2020-12-01 12:21

In the thread What’s your favorite “programmer ignorance” pet peeve?, the following answer appears, with a large amount of upvotes:

Programmers who build XML u

相关标签:
12条回答
  • You can end up with invalid XML, but you will not find out until you parse it again - and then it is too late. I learned this the hard way.

    0 讨论(0)
  • 2020-12-01 12:59

    The main reason is DRY: Don't Repeat Yourself.

    If you use string concat to do XML, you will constantly be repeating the functions that keep your string as a valid XML document. All the validation would be repeated, or not present. Better to rely on a class that is written with XML validation included.

    0 讨论(0)
  • 2020-12-01 13:01

    As you said, it's just awkward to build XML correct using string concatenation, especially now you have XML linq that allows for simple construction of an XML graph and will get namespaces, etc correct.

    Obviously context and how it is being used matters, such as in the logging example string.Format can be perfectly acceptable.

    But too often people ignore these alternatives when working with complex XML graphs and just use a StringBuilder.

    0 讨论(0)
  • 2020-12-01 13:04

    I've always found creating an XML to be more of a chore than reading in one. I've never gotten the hang of serialization - it never seems to work for my classes - and instead of spending a week trying to get it to work, I can create an XML file using strings in a mere fraction of the time and write it out.

    And then I load it in using an XMLReader tree. And if the XML file doesn't read as valid, I go back and find the problem within my saving routines and corret it. But until I get a working save/load system, I refuse to perform mission-critical work until I know my tools are solid.

    I guess it comes down to programmer preference. Sure, there are different ways of doing things, for sure, but for developing/testing/researching/debugging, this would be fine. However I would also clean up my code and comment it before handing it off to another programmer.

    Because regardless of the fact you're using StringBuilder or XMLNodes to save/read your file, if it is all gibberish mess, nobody is going to understand how it works.

    0 讨论(0)
  • 2020-12-01 13:05

    I think readability, flexibility and scalability are important factors. Consider the following piece of Linq-to-Xml:

    XDocument doc = new XDocument(new XDeclaration("1.0","UTF-8","yes"),
       new XElement("products", from p in collection
        select new XElement("product",
            new XAttribute("guid", p.ProductId), 
            new XAttribute("title", p.Title),
            new XAttribute("version", p.Version))));
    

    Can you find a way to do it easier than this? I can output it to a browser, save it to a document, add attributes/elements in seconds and so on ... just by adding couple lines of code. I can do practically everything with it without much of effort.

    0 讨论(0)
  • 2020-12-01 13:09

    I think that the problem is that you aren't watching the xml file as a logical data storage thing, but as a simple textfile where you write strings.

    It's obvious that those libraries do string manipulation for you, but reading/writing xml should be something similar to saving datas into a database or something logically similar

    0 讨论(0)
提交回复
热议问题