【问题标题】:xlinq throw null data? how can i solve it?xlinq 抛出空数据?我该如何解决?
【发布时间】:2009-07-03 18:11:17
【问题描述】:

你好;我尝试从 xml 文件中保存 xml 数据。我怎样才能做到这一点?如果我使用下面的代码,XmlStream 返回 null “throw null”。怎么能这样?

我的数据:


<list>
  <subscriber Type="Random">
    <name>yusuf</name>
    <surname>karatoprak</surname>
  </subscriber>
</list>

 public static XDocument GetRawsSnippetAsXDocuments()
       {
           Assembly assembly = Assembly.GetExecutingAssembly();
           Stream xmlStream = assembly.GetManifestResourceStream("XlinqTest1.User.xml");
           if (xmlStream == null)
           {
               throw new NullReferenceException("Hata!!!");
           }
           XDocument xmlDocument = XDocument.Load(XmlReader.Create(xmlStream));

           return xmlDocument;
       }

主程序:

 class Program
    {
        static void Main(string[] args)
        {
            XDocument document = XmlHelper.GetRawsSnippetAsXDocuments();
            var listCount = document.Descendants("list").Count();
        }
    }

【问题讨论】:

    标签: c# .net asp.net linq


    【解决方案1】:

    您错误地加载了程序集资源。除了 User.xml,您还需要 yourNamespace.User.xml。如果您的资源位于项目内的文件夹中,那么最终名称也会反映出来。

    Check out this example.

    【讨论】:

    • 您是否使用设置为嵌入资源的 XML 文件进行编译?
    猜你喜欢
    • 2022-07-12
    • 2012-12-03
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多