2011-05-24 85 views
0

我需要用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 

什麼可能是錯誤的?

回答

5

替換此:

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

2

在你的解析方法,你想

XDocument.Parse(input) 
1

更換

XDocument xdoc = XDocument.Load(input); 

XDocument xdoc = XDocument.Parse(input);