在C#中的用户界面上显示之前修改xml值
本文关键字:修改 xml 显示 用户界面 | 更新日期: 2023-09-27 18:21:28
我的c#程序读取一个xml并将其加载到xmldocument中。我需要解密银行账号和排序码元素,然后才能在用户界面中显示。这是xml。
<Fatca xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/TaskDetails.xsd">
<AccountNumber>BI830418</AccountNumber>
<AccountDetails>
<AccountName>SIPP - Mr. t test</AccountName>
<AccountNumber>BI830418</AccountNumber>
<AccountID>83041</AccountID>
<BankAccountDetails>
<BankAccountID>23943</BankAccountID>
<ContactID>2106175</ContactID>
<BankAccountName>dffdf</BankAccountName>
<BankAccountNumber>N14yKOOmpdmh23fmp7oNvg==</BankAccountNumber>
<BankAccountType>0</BankAccountType>
<BankSortCode>tz7r+uYFL6Ff86mI/mwJOQ==</BankSortCode>
<Active>true</Active>
</BankAccountDetails>
<Active>true</Active>
</AccountDetails>
<Request>
<AccountID>83041</AccountID>
</Request>
</Fatca>
我的c代码逻辑如下。我得到对象引用错误。你能告诉我这里错在哪里吗?
document.XPathSelectElement("//BankAccountDetails/BankAccountNumber").Value="Test";
命名空间给您带来了问题。我更喜欢使用XMLLinq(XDocument)。它将更容易排序和过滤
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<Fatca xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" xmlns:xsd='"http://www.w3.org/2001/XMLSchema'" xmlns='"http://tempuri.org/TaskDetails.xsd'">" +
"<AccountNumber>BI830418</AccountNumber>" +
"<AccountDetails>" +
"<AccountName>SIPP - Mr. t test</AccountName>" +
"<AccountNumber>BI830418</AccountNumber>" +
"<AccountID>83041</AccountID>" +
"<BankAccountDetails>" +
"<BankAccountID>23943</BankAccountID>" +
"<ContactID>2106175</ContactID>" +
"<BankAccountName>dffdf</BankAccountName>" +
"<BankAccountNumber>N14yKOOmpdmh23fmp7oNvg==</BankAccountNumber>" +
"<BankAccountType>0</BankAccountType>" +
"<BankSortCode>tz7r+uYFL6Ff86mI/mwJOQ==</BankSortCode>" +
"<Active>true</Active>" +
"</BankAccountDetails>" +
"<Active>true</Active>" +
"</AccountDetails>" +
"<Request>" +
"<AccountID>83041</AccountID>" +
"</Request>" +
"</Fatca>";
XElement fatca = XElement.Parse(xml);
XNamespace ns = fatca.Name.Namespace;
string bankAccountNumber = fatca.Descendants(ns + "BankAccountNumber").FirstOrDefault().Value;
}
}
}
您需要添加名称空间。
XPathSelectElement的信用总是返回空
var xmlReader = XmlReader.Create(new StringReader(xml)); // Or whatever your source is, of course.
var myXDocument = XDocument.Load(xmlReader);
var namespaceManager = new XmlNamespaceManager(xmlReader.NameTable);
namespaceManager.AddNamespace("prefix", "http://tempuri.org/TaskDetails.xsd"); // We add an explicit prefix mapping for our query.
XElement fatca = XElement.Parse(xml);
fatca.XPathSelectElement("//prefix:BankAccountNumber", namespaceManager).Value = "asd";
Console.WriteLine(fatca.ToString());