我想读取xml文件,但是由于Document节点属性,它没有读取文件。
Code C#:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(HttpContext.Server.MapPath("~/Content/Images/MMS-CREATE-ALLA-ALLAH2H1-23102018-000170-INP.xml"));
XmlNode settings = xmldoc.SelectSingleNode("Document[@xmlns='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/MndtInitnReq/GrpHdr");
stu.BranchName = settings.SelectSingleNode("MsgId").InnerText;
XML FIle:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.009.001.01">
<MndtInitnReq>
<GrpHdr>
<MsgId>10005226074</MsgId>
<CreDtTm>2018-10-23T15:20:56</CreDtTm>
</GrpHdr>
</MndtInitnReq>
</Document>
答案 0 :(得分:0)
您具有必须用于获取数据的名称空间。尝试Xml Linq:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication75
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XNamespace ns = doc.Root.GetDefaultNamespace();
string msgId = (string)doc.Descendants(ns + "MsgId").FirstOrDefault();
XElement xCreDtTm = doc.Descendants(ns + "CreDtTm").FirstOrDefault();
//will give 1/1/01 when null
DateTime CreDtTm = xCreDtTm == null ? new DateTime() : (DateTime)xCreDtTm;
}
}
}
答案 1 :(得分:0)
我认为加载此xml应该不成问题。我通过加载您在XmlDocument对象中发布的xml进行了验证。 但是我认为,要获取“设置”节点的xpath应该在Document之后的所有标签中都具有xml名称空间。 因此,xpath应该为“ / [local-name()='Document'和namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01 '] / [local-name()='MndtInitnReq'和namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01'] / * [local -name()='GrpHdr'和namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01'] “