Linq to xml 读取xml文件

<textarea cols="50" rows="15" name="code" class="xhtml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt; &lt;UIConfig Count=&quot;1&quot;&gt; &lt;workflow name=&quot;OilInbound&quot;&gt; &lt;activity name=&quot;Started&quot;&gt; &lt;Input&gt; &lt;Page&gt;&lt;/Page&gt; &lt;Method&gt;&lt;/Method&gt; &lt;/Input&gt; &lt;Brief&gt; &lt;Page&gt;OilInboundTaskDetail&lt;/Page&gt; &lt;Method&gt;GetTaskDetailModel&lt;/Method&gt; &lt;/Brief&gt; &lt;Detail&gt; &lt;Page&gt;OilInboundTaskDetail&lt;/Page&gt; &lt;Method&gt;GetTaskDetailModel&lt;/Method&gt; &lt;/Detail&gt; &lt;DisplayName&gt;任务创建&lt;/DisplayName&gt; &lt;/activity&gt; &lt;activity name=&quot;OilCompositionAnalysis&quot;&gt; &lt;Input&gt; &lt;Page&gt;OilAnalyzingDataInput&lt;/Page&gt; &lt;Method&gt;GetOilAnalyzingDataInputModel&lt;/Method&gt; &lt;/Input&gt; &lt;Brief&gt; &lt;Page&gt;OilAnalyzingDataBrief&lt;/Page&gt; &lt;Method&gt;GetOilAnalyzingDataBriefModel&lt;/Method&gt; &lt;/Brief&gt; &lt;Detail&gt; &lt;Page&gt;OilAnalyzingDataDetail&lt;/Page&gt; &lt;Method&gt;GetOilAnalyzingDataDetailModel&lt;/Method&gt; &lt;/Detail&gt; &lt;DisplayName&gt;化验&lt;/DisplayName&gt; &lt;/activity&gt; &lt;/workflow&gt; &lt;/UIConfig&gt;</textarea>

 

<textarea cols="50" rows="15" name="code" class="c-sharp">using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Xml.Linq; using System.Xml; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { XElement doc = XElement.Load(&quot;..//..//data-config.xml&quot;);//根元素 string sss = getStr(doc, &quot;OilInbound&quot;, &quot;Started&quot;, &quot;Input&quot;, &quot;Page&quot;); Console.WriteLine(sss); Console.ReadKey(); } //如果当前元素的子元素只有一个,可以直接用.Element(&quot;elementName&quot;)来得到,如果是多个子元素就用Elements(&quot;elementName&quot;)得到 private static string getStr(XElement doc,string workflowName,string stepName,string typeName,string pageMethod) { var strEle = from workflow in doc.Elements(&quot;workflow&quot;) where (string)workflow.Attribute(&quot;name&quot;) == workflowName select new{ str = workflow.Elements(&quot;activity&quot;).TakeWhile(x =&gt; (string)x.Attribute(&quot;name&quot;) == stepName).First().Element(typeName).Element(pageMethod).Value }; return strEle.First().str; } } }</textarea>

 

你可能感兴趣的:(xml,workflow,String,input,LINQ,encoding)