我试图编写一个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的文档路径是正确的,并且xml中存在值“ aaaaE31tc63qjPfIMJ”,但是测试出现错误,消息为System.NullReferenceException : Object reference not set to an instance of an object.
在类PORReader
中,root
的值为null,我需要它成为从XmlDocumentParser
导入的对象。我不熟悉C#类,因此不胜感激
谢谢!
答案 0 :(得分:1)
我知道了。
您不会在XmlDocumentParser
构造函数中执行任何操作。
此行:
XmlNode Root = document.DocumentElement;
不分配给Root
属性,它创建一个名为Root
的局部变量。更改为
Root = document.DocumentElement;
,看看是否有帮助。
同样,使用调试器运行并查看一切是否正确,例如如果document.DocumentElement
实际上返回了您要查找的根。
编辑:
我是盲人。
您在PORReader
中有完全相同的错误。
var root = xmlDocumentParser.Root;
这不会分配给您的private readonly
字段,而是创建一个局部变量。更改为:
root = xmlDocumentParser.Root;
答案 1 :(得分:0)
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; }
}
}