Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在类之间传递对象为null_C# - Fatal编程技术网

C#在类之间传递对象为null

C#在类之间传递对象为null,c#,C#,我试图编写一个C#应用程序,解析XML文件并通过函数返回所选节点。这些是我的课程: XmlDocumentParser.cs using System; using System.Text; using System.IO; using System.Xml; namespace Namespace1 { public class XmlDocumentParser { // This is a private instance variable that ca

我试图编写一个C#应用程序,解析XML文件并通过函数返回所选节点。这些是我的课程:

XmlDocumentParser.cs

using System;
using System.Text;
using System.IO;
using System.Xml;

namespace Namespace1
{
    public class XmlDocumentParser
    {
        // This is a private instance variable that cannot be reassigned
        private readonly XmlDocument document = new XmlDocument();
        private string udc = "urn:rosettanet:specification:universal:Document:xsd:schema:01.12";
        private string udct = "urn:rosettanet:specification:universal:DocumentType:xsd:codelist:01.13";
        private string tns = "urn:rosettanet:specification:interchange:PurchaseOrderRequest:xsd:schema:02.05";
        private string dp = "urn:rosettanet:specification:domain:Procurement:xsd:schema:02.29";

        //This is the constructor
        public XmlDocumentParser(string documentPath)
        {
            document.Load(documentPath);
            XmlNode Root = document.DocumentElement;
            XmlNamespaceManager Nsmgr = new XmlNamespaceManager(document.NameTable);
            Nsmgr.AddNamespace("udc", udc); 
            Nsmgr.AddNamespace("udct", udct);
            Nsmgr.AddNamespace("tns", tns);
        }

        // This is a public property that can only be read and not set
        public XmlNode Root { private set; get; }
        public XmlNode Nsmgr {private set; get; }
    }
}
PORReader.cs

using System;
using System.Text;
using System.IO;
using System.Xml;


namespace Namespace1 {
    public class PORReader {
    private readonly XmlNode root;
    private readonly XmlNamespaceManager nsmgr;
    public PORReader(string documentPath){ 
        var xmlDocumentParser = new XmlDocumentParser(documentPath);
        var root = xmlDocumentParser.Root;  
    }

    public object getPurchaseOrder(){

        XmlNode node = root.SelectSingleNode(
                "descendant::tns:PurchaseOrder/udc:BusinessDocumentReference[udct:DocumentType='SAO']/udc:Identifier", nsmgr);
        return returnNode(node.InnerXml);
    }

        private object returnNode(object node){
            if (node != null) {
                return node;
            } else {
                return "Error in Node";
            }
        }
    }
}
Program.cs:

using System;
using System.Text;
using System.IO;
using System.Xml;

namespace Namespace1
{
    class Program
    {        
        static void Main(string[] args){
            // This will reside in the reader classes
            var xmlDocumentParser = new XmlDocumentParser("/Users/moorel/Desktop/Projects/C#/O2/DummyFiles/SupplyChainSourceFiles/POR/POR_SALES_8307_20180201164154.xml");
            var root = xmlDocumentParser.Root;
            var nsmgr = xmlDocumentParser.Nsmgr;
        }
    }
}
我的测试是:

namespace Namespace1 {
    using NUnit.Framework;

    [TestFixture]
    public class PORReaderTest {
        [Test]
        public void getPurchaseOrder(){

            PORReader reader = new PORReader("/Users/moorel/Desktop/Projects/C#/O2/DummyFiles/SupplyChainSourceFiles/POR/POR_SALES_8307_20180201164154.xml");
            Assert.AreEqual(reader.getPurchaseOrder(), "aa036E31tc63qjPfIMJ");
        }
    }
}
因此,xml的文档路径是正确的,值“aa036E31tc63qjPfIMJ”存在于xml中,但是测试出错,消息为
System.NullReferenceException:Object reference未设置为对象的实例。

在类
PORReader
中,
root
的值为null,我需要它是从
XmlDocumentParser
导入的对象。我不熟悉C类,因此非常感谢您的帮助


谢谢!

XmlDocumentParser.cs

namespace Namespace1
{
    public class XmlDocumentParser
    {
        // This is a private instance variable that cannot be reassigned
        private readonly XmlDocument document = new XmlDocument();
        private string udc = "urn:rosettanet:specification:universal:Document:xsd:schema:01.12";
        private string udct = "urn:rosettanet:specification:universal:DocumentType:xsd:codelist:01.13";
        private string tns = "urn:rosettanet:specification:interchange:PurchaseOrderRequest:xsd:schema:02.05";
        private string dp = "urn:rosettanet:specification:domain:Procurement:xsd:schema:02.29";

        //This is the constructor
        public XmlDocumentParser(string documentPath)
        {
            document.Load(documentPath);
            Root = document.DocumentElement as XmlNode;
            Nsmgr = new XmlNamespaceManager(document.NameTable);
            Nsmgr.AddNamespace("udc", udc);
            Nsmgr.AddNamespace("udct", udct);
            Nsmgr.AddNamespace("tns", tns);
        }

        // This is a public property that can only be read and not set
        public XmlNode Root { get; }
        public XmlNamespaceManager Nsmgr { get; }
    }
}
哦,我明白了

您在
XmlDocumentParser
构造函数中没有实际执行任何操作

这一行:

XmlNode Root = document.DocumentElement;
不分配给
Root
属性,它会创建一个名为
Root
的局部变量。将其更改为

Root = document.DocumentElement;
看看这是否有帮助。 另外,明智的做法是运行调试器并查看所有内容是否正确,例如,如果
document.DocumentElement
实际返回您要查找的根目录

编辑:

我瞎了

您在
PORReader
中有完全相同的错误

var root = xmlDocumentParser.Root;
这不会分配给您的
私有只读
字段,而是创建一个局部变量。更改为:

root = xmlDocumentParser.Root;

如果您不熟悉,为什么要用C#实现它?如果没有看到XML文档,我们可能帮不了什么忙。您需要向我们展示您试图解析的XML文档。不幸的是,我无法共享XML文档,但xpath工作正常,测试中提供了值。我在从XmlDocumentPar传递根对象时遇到问题ser类更改为PORReader类。我相应地更改了XmlDocumentParser.cs,错误仍然存在stacktrace说什么?`错误消息:System.NullReferenceException:对象引用未设置为对象的实例。堆栈跟踪:位于Namespce1.PORReader.getPurchaseOrder()在/Users/moorel/Desktop/Projects/C#/O2/ReportingXML/PORReader.cs:Namespace1.PORReaderTest.getPurchaseOrder()的第18行在/Users/moorel/Desktop/Projects/C#/O2/ReportingXML/PORReaderTest.cs中:第11行`感谢您的响应,我进行了这些编辑,但是错误消息'System.NullReferenceException:Object reference未设置为对象的实例。`仍然存在:/是否运行了调试器?