[英]How to query from XML with SchemaVersion?
我有這個XML檔案
<?xml version="1.0" encoding="us-ascii" ?>
<LoanSetupFile SchemaVersion="3.0" NumberOfLoans="2" xmlns="https://www.something.com/schemas">
<Loan ServicerLoanNumber="1"/>
<Loan ServicerLoanNumber="2"/>
</LoanSetupFile>
我正在嘗試獲取ServicerLoanNumber屬性。 我已經嘗試了很多方法,這可能是我最接近的嘗試,但是仍然返回null
var doc = XDocument.Load("fileName.xml");
XNamespace ns = doc.Root.GetDefaultNamespace(); //This gives me the correct namespace
var loans = from l in doc.Descendants(ns+"Loan") select new { ServicerLoanNumber = l.Attribute("ServicerLoanNumber").Value };
有誰知道這是怎么回事? 我是否需要包含SchemaVersion? 如果是這樣,怎么辦?
非常感謝..
我只是在winform中對您的代碼進行了快速測試,效果很好。
StringBuilder sb = new StringBuilder();
sb.Append(@"<?xml version=""1.0"" encoding=""us-ascii"" ?>");
sb.Append(@"<LoanSetupFile SchemaVersion=""3.0"" NumberOfLoans=""2"" xmlns=""https://www.something.com/schemas"">");
sb.Append(@"<Loan ServicerLoanNumber=""1""/>");
sb.Append(@"<Loan ServicerLoanNumber=""2""/>");
sb.Append(@"</LoanSetupFile>");
var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString() ?? "")));
XNamespace ns = doc.Root.GetDefaultNamespace(); //This gives me the correct namespace
var loans = from l in doc.Descendants(ns + "Loan")
select new { ServicerLoanNumber = l.Attribute("ServicerLoanNumber").Value };
// Temporarily display the values
foreach (var l in loans)
{
MessageBox.Show(l.ServicerLoanNumber);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.