为什么从XDocument获取元素会导致null?

Edw*_*uay 3 c# linq linq-to-xml

任何人都可以向我解释为什么xml1.Element("title")正确等于" <title>Customers Main333</title>"但xml2.Element("title")令人惊讶地等于null,即为什么我必须将XML文档作为元素而不是文档按顺序获取从中拉出元素?

var xml1 = XElement.Load(@"C:\\test\\smartForm-customersMain.xml");
var xml2 = XDocument.Load(@"C:\\test\\smartForm-customersMain.xml");

string title1 = xml1.Element("title").Value;
string title2 = xml2.Element("title").Value;
Run Code Online (Sandbox Code Playgroud)

XML:

<?xml version="1.0" encoding="utf-8" ?>
<smartForm idCode="customersMain">
    <title>Customers Main333</title> 
    <description>Generic customer form.</description>
    <area idCode="generalData" title="General Data">
        <column>
            <group>
                <field idCode="anrede">
                    <label>Anrede</label>
                </field>
                <field idCode="firstName">
                    <label>First Name</label>
                </field>
                <field idCode="lastName">
                    <label>Last Name</label>
                </field>
            </group>
        </column>
    </area>
    <area idCode="address" title="Address">
        <column>
            <group>
                <field idCode="street">
                    <label>Street</label>
                </field>
                <field idCode="location">
                    <label>Location</label>
                </field>
                <field idCode="zipCode">
                    <label>Zip Code</label>
                </field>
            </group>
        </column>
    </area>
</smartForm>
Run Code Online (Sandbox Code Playgroud)

Chr*_*ser 9

XDocument 表示整个文档,而不是根节点.用Root得到的根元素.

var title = xml2.Root.Element("title").Value; 
Run Code Online (Sandbox Code Playgroud)

应该管用.