处理Linq XML字符串

问题描述:

我需要用Linq解析XML字符串,并且我想出了以下代码。处理Linq XML字符串

using System; 
using System.Linq; 
using System.Xml.Linq; 

class LinqXml 
{ 
    public void Parse(string input) 
    { 
    XDocument xdoc = XDocument.Load(input); 
    var lang = from d in xdoc.Elements("PipeUnit").Elements("Step").Elements("Pipelist").Elements("NamedPipe").Elements("NameOfPipe") select d; 

    Console.WriteLine(lang.First().Value); 

    foreach (var item in lang) 
    { 
     Console.WriteLine(item.Value); 
    } 
    } 

    static void Main() 
    { 

     string tempString = @" 
<PipeUnit> 
    <Step> 
    <Pipelist> 
     <NamedPipe> 
     <NameOfPipe>Name</NameOfPipe> 
     <PipeData>Data</PipeData> 
     </NamedPipe> 
    </Pipelist> 
    </Step> 
</PipeUnit>   
     "; 
     var linqXml = new LinqXml(); 
     linqXml.Parse(tempString); 
    } 
} 

当编译这段代码使用Mono - dmcs linqxml.cs /r:System.Xml.Linq.dll,并试图逃跑,我得到了以下错误。

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path "/Users/smcho/Desktop/csharp/ 
<PipeUnit> 
    <Step> 
    <Pipelist> 
     <NamedPipe> 
     <NameOfPipe>Name</NameOfPipe> 
     <PipeData>Data</PipeData> 
     </NamedPipe> 
    </Pipelist> 
    </Step> 
<PipeUnit>". 
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0 
    at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) 
    at System.Xml.XmlUrlResolver.GetEntity (System.Uri absoluteUri, System.String role, System.Type ofObjectToReturn) [0x00000] in <filename unknown>:0 
    at Mono.Xml2.XmlTextReader.GetStreamFromUrl (System.String url, System.String& absoluteUriString) [0x00000] in <filename unknown>:0 
    at Mono.Xml2.XmlTextReader..ctor (Boolean dummy, System.Xml.XmlResolver resolver, System.String url, XmlNodeType fragType, System.Xml.XmlParserContext context) [0x00000] in <filename unknown>:0 
    at System.Xml.XmlTextReader..ctor (Boolean dummy, System.Xml.XmlResolver resolver, System.String url, XmlNodeType fragType, System.Xml.XmlParserContext context) [0x00000] in <filename unknown>:0 
    at System.Xml.XmlReader.Create (System.String url, System.Xml.XmlReaderSettings settings, System.Xml.XmlParserContext context) [0x00000] in <filename unknown>:0 
    at System.Xml.XmlReader.Create (System.String url, System.Xml.XmlReaderSettings settings) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options) [0x00000] in <filename unknown>:0 
    at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <filename unknown>:0 
    at LinqXml.Parse (System.String input) [0x00000] in <filename unknown>:0 
    at LinqXml.Main() [0x00000] in <filename unknown>:0 

什么可能是错误的?

替换此:

XDocument xdoc = XDocument.Load(input); 

有:

XDocument xdoc = XDocument.Parse(input); 

你逝去的XML,而不是一个文件名。

+0

我的错误甚至parse()方法。 – prosseek 2011-05-24 16:25:15

+1

这是因为你的XML是无效的:最后应该是''而不是'' - 这个例外你会得到详细信息。 – BrokenGlass 2011-05-24 16:27:39

+0

是的,这是正确的。谢谢。 – prosseek 2011-05-24 16:31:07

在你的解析方法,你想

XDocument.Parse(input) 

更换

XDocument xdoc = XDocument.Load(input); 

XDocument xdoc = XDocument.Parse(input);