简体   繁体   中英

How I get a Element from a XML File

I want to get a Element from a XML File. But I don't know how I get a Element from The XML.

My XML File:

<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="resources">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="id" type="xs:string" minOccurs="0" />
                <xs:element name="text" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <resources>
    <id>id_SearchUser</id>         <!--Label lblSearchUser-->
    <text>Benutzer</text>
  </resources>
  <resources>
    <id>id_location</id>           <!--Label lblLocation-->
    <text>Werk</text>
  </resources>
  <resources>
    <id>id_Search</id>             <!--Button Button2-->
    <text>Suchen</text>
  </resources>
  <resources>
    <id>id_User</id>                <!--Label lblUser-->
    <text>Benutzer</text>
  </resources>
  <resources>
    <id>id_eMail</id>               <!-- Label lblEmail-->
    <text>eMail</text>
  </resources>
  <resources>
    <id>id_Firstname</id>           <!--LinkLabel lnkFirstname-->
    <text>Vorname</text>
  </resources>
  <resources>
    <id>id_Lastname</id>           <!--LinkLabel lnkLastname-->
    <text>Nachname</text>
  </resources>
  <resources>
    <id>id_Telephonnumber</id>     <!--Label lblTelephon-->
    <text>Telefon</text>
  </resources>
</NewDataSet>

My Idea:

I get the location of a User. For Example "de" and than I want to call the GetXMLElement Method that give me the Content of a Element.

For Example:

I have a Label in my Application "id_SearchUser" and I want that this text property change to the Content of the ...

...

<resources>
    <id>id_SearchUser</id>        
    <text>Benutzer</text>
</resources>

....

and the Text is than Benutzer.

My Method:

public string GetXMLElement()
{
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(Server.MapPath("~/App_Data/de_language.xml"));
    XmlNode node = // ??
    return // node as string
}

What about this?

XDocument document = XDocument.Load(Server.MapPath("~/App_Data/de_language.xml"));
XElement element = document.Root.Elements("resources").FirstOrDefault(e => e.Element("id").Value == "id_SearchUser");
string text;
if (element != null)
  text = element.Element("text").Value;

Notice that I used XDocument instead of your XmlDocument , XDocument uses LINQ whereas XmlDocument uses XPATH to get nodes. It's a matter of preference.

You can use XQuery to get the element you want:

Here is a quick example:

XPathDocument doc = new XPathDocument(Server.MapPath("~/App_Data/de_language.xml"));
var nav = doc.CreateNavigator();
var nodes = nav.select("/xpath/to/the/node/you/want/to/select"); // for example /resources/id/text
nodes.First();
// now nodes.Current.Value is the value of the node you've just selected.

You'll want to use xpath to select the nodes you need. The xpath reference on MSDN is an excellent resource for this.

You then need to use the SelectSingleNode method to find that node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM