简体   繁体   中英

How to Read CSHTML as XML file in C#

I have a cshtml file in this file I have a hidden field and static value. I want to read it as a XML file to find the hidden field. As a matter of fact, I don't like open it as text file and search into it.

This is a part of my file and I want to read hidden file:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" content="Soha Application" />
    <title>@ViewBag.MeetingTitle</title>    
    <link href="@Url.Content("~/Content/themes/Default/main.css")" rel="stylesheet" type="text/css" />    
    <link href="@Url.Content("~/Content/themes/Default/fa-IR.css")" rel="stylesheet" type="text/css" />    
    <script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <header>
            **<input id="SkinId" type="hidden" value="008BBF4768861284321640F895D68DA1" /> <!-- Please dont change this Code -->**
            <div id="title">
                <h1>@ViewBag.MeetingTitle</h1>
            </div>
            <nav>
            </nav>
        </header>

I'm using the XPath extension from here: https://github.com/ChuckSavage/XmlLib/

This should work for you. It will return all elements that have an attribute type="hidden"

XElement root = XElement.Load(file); // or XElement.Parse(string);
var hidden = root.XPath("//*[@type='hidden']");

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