正在处理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,而不是文件名。

在您的Parse方法中,您需要

XDocument.Parse(input)

更换

XDocument xdoc = XDocument.Load(input);

带有

XDocument xdoc = XDocument.Parse(input);

相关内容

  • 没有找到相关文章

最新更新