tôi có mã này:Sử dụng LINQ to XML với namespace Xml
/*string theXml =
@"<Response xmlns=""http://myvalue.com""><Result xmlns:a=""http://schemas.datacontract.org/2004/07/My.Namespace"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:TheBool>true</a:TheBool><a:TheId>1</a:TheId></Result></Response>";*/
string theXml = @"<Response><Result><TheBool>true</TheBool><TheId>1</TheId></Result></Response>";
XDocument xmlElements = XDocument.Parse(theXml);
var elements = from data in xmlElements.Descendants("Result")
select new {
TheBool = (bool)data.Element("TheBool"),
TheId = (int)data.Element("TheId"),
};
foreach (var element in elements)
{
Console.WriteLine(element.TheBool);
Console.WriteLine(element.TheId);
}
Khi tôi sử dụng giá trị đầu tiên cho theXml, kết quả là null, trong khi với cái thứ hai, tôi có giá trị tốt đẹp ...
Cách sử dụng LINQ to Xml với giá trị xmlns?
tuyệt vời, làm việc :) !! Cảm ơn rất nhiều – Tim
xmlElements.Descendants (xmlElements.Root.GetDefaultNamespace() + "Result") cũng có thể hoạt động ... –