Consider Replacing XmlDocument with XElement

Please follow this template to help us specify this new rule:

  • description of the Rule:

  • Using XmlDocument can result in the following issues.

    Resource consumption. Processing large XML documents can cause high CPU, memory, and bandwidth utilization. XML is a verbose and text-based representation. Therefore, XML documents are larger than binary representations of the same data. These issues are magnified if users access your application over low-bandwidth networks, or if many users connect to the application at the same time.

    Extreme memory consumption. Using the DOM-model and the XmlDocument or XPathDocument classes to parse large XML documents can place significant demands on memory. These demands may severely limit the scalability of server-side Web applications.

    Inefficient transformations. Choosing the wrong transformation approach, building inefficient XPath queries, or processing large and poorly-structured source XML files can affect the performance of your application’s transformation logic. You should transform application data to XML just before the data leaves the boundaries of the application. Keeping the data in binary format is the most efficient way to perform data manipulations.
    Using XElement or XDocument will result in better performace. LINQ to XML is an in-memory XML programming interface that enables you to modify XML documents efficiently and easily. For support of LINQ to XML these XML types were completely rewritten with performance in mind.

  • snippet of Noncompliant Code

  • snippet of Compilant Code (fixing the above noncompliant code)

  • Page 32 of 57
    Consider Replacing XmlDocument with XElement
    Guideline
    Why
    Using XmlDocument can result in the following issues.

    Resource consumption. Processing large XML documents can cause high CPU, memory, and bandwidth utilization. XML is a verbose and text-based representation. Therefore, XML documents are larger than binary representations of the same data. These issues are magnified if users access your application over low-bandwidth networks, or if many users connect to the application at the same time.

    Extreme memory consumption. Using the DOM-model and the XmlDocument or XPathDocument classes to parse large XML documents can place significant demands on memory. These demands may severely limit the scalability of server-side Web applications.

    Inefficient transformations. Choosing the wrong transformation approach, building inefficient XPath queries, or processing large and poorly-structured source XML files can affect the performance of your application’s transformation logic. You should transform application data to XML just before the data leaves the boundaries of the application. Keeping the data in binary format is the most efficient way to perform data manipulations.
    Using XElement or XDocument will result in better performace. LINQ to XML is an in-memory XML programming interface that enables you to modify XML documents efficiently and easily. For support of LINQ to XML these XML types were completely rewritten with performance in mind.
    When
    Use XElement and XDocument instead of XmlDocument to represent Xml Elements and Documents. Consider replacing usage of XmlDocument with XElement or XDocument as appropriate.
    How
    You can create an XML Tree using XElement as follows:

XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
  • type : Bug