2-layer XML Read class

Source: Internet
Author: User

Configuration file

<?xml>

<root>

<parent name= "C" >

<child name= "C1" >Spring</child>

<child name= "C2" >Summer</child>

</parent>

<parent name= "D" >

<child name= "D1" >autome</child>

<child name= "D2" >winter</child>

</parent>

</root>

Calling methods

var xmllevel2helper=new xmllevel2helper ("parent", "Child");

var value=xmllevel2helper.getnodevalue ("D", "D2")

public class Xmllevel2helper
{
Private readonly String _parentnodename = "Controller";
Private readonly String _childnodename = "Action";

Public Xmllevel2helper ()
{

}
Public Xmllevel2helper (String parentnodename, String childnodename)
{
_parentnodename = Parentnodename;
_childnodename = Childnodename;
}

<summary>
Reads the value of the 2-layer XML file node.
</summary>
<param name= "FilePath" > such as ~/config/customconfig.xml</param>
<param name= "Pnode" > First layer node name </param>
<param name= "CNode" > Second layer node name </param>
<returns></returns>
public string Getnodevalue (String filepath,string pnode, String cNode)
{
XElement rootelement = Xelement.load (HttpContext.Current.Server
. MapPath (FilePath));
XElement pelement = Findelementbyattribute (rootelement, _parentnodename, CNode);
if (pelement! = null)
{
XElement cElement = Findelementbyattribute (pelement, _childnodename, Pnode);
if (cElement! = null)
{
return celement.value;
}
}
Return "";
}


<summary>
Find the appropriate child elements according to attribute
</summary>
<param name= "XElement" ></param>
<param name= "TagName" ></param>
<param name= "attribute" ></param>
<returns></returns>

Private XElement Findelementbyattribute (XElement XElement, String tagName, string attribute)
{
Return Xelement.elements (TagName). FirstOrDefault
(x = X.attribute ("name"). Value.equals (attribute, stringcomparison.ordinalignorecase));
}
}

2-layer XML Read class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.