简体   繁体   English

使用XmlNode进行XML解析

[英]XML Parsing using XmlNode

In My application, I'm getting an XML Response and I'm trying to convert that XML Response using XmlNode . 在我的应用程序中,我得到一个XML Response并且试图使用XmlNode转换该XML响应。

My XML File : File 我的XML文件: 文件

The XML Content will be like : XML内容将类似于:

<account name="santosh@mx.omit-solutions.com" id="876e6b55-5a9c-44ff-a418-af737c47d2af">
  <a n="mobile">91 998977245009</a>
  <a n="zimbraMailDeliveryAddress">santosh@mx.omit-solutions.com</a>
  <a n="zimbraPrefIMLogChats">TRUE</a>
</account>
<account name="mani@mx.omit-solutions.com" id="ce91d3bf-83b8-4a55-a92e-2e080fa4a21b">
  <a n="zimbraMailDeliveryAddress">mani@mx.omit-solutions.com</a>
  <a n="zimbraPrefShowSearchString">FALSE</a>
  <a n="zimbraPrefIMLogChats">TRUE</a>
</account>

Here I'm showing two sample accounts from that XML Response and I need to loop through all the account nodes and get the zimbraMailDeliveryAddress which will be surely available in every node and also mobile element value ('91 998977245009') which may not be available in every node(ie the tag wont be available at all). 在这里,我展示了该XML响应中的两个示例帐户,我需要遍历所有account nodes并获得zimbraMailDeliveryAddress肯定会在每个节点中使用)以及mobile元素值('91 998977245009') (可能不可用)在每个节点中(即标记将根本不可用)。

Is there a way to get these values? 有没有办法获得这些价值?

I've tried using the XmlNamespaceManager like this : 我试过像这样使用XmlNamespaceManager

XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("bc", "urn:zimbraAdmin");
XmlNode result = doc.SelectSingleNode("//bc:account", mgr);
if (result != null)
{
    Console.WriteLine("Found {0}.", result.InnerText);
}
else
{
    // handle case that no element was found
    Console.WriteLine("No element found.");
}

But by using this code, I'm getting only first account node data and I'm unable to loop through all account nodes . 但是通过使用此代码,我仅获得first account node数据,而无法遍历all account nodes

Can anyone please help me? 谁能帮帮我吗?

I'd suggest ditching XmlDocument and XPath and using LINQ to XML instead: 我建议放弃XmlDocument和XPath并改用LINQ to XML:

var doc = XDocument.Parse(xml);

XNamespace ns = "urn:zimbraAdmin";

foreach (var account in doc.Descendants(ns + "account"))
{
    var mobile = (string)account.Elements(ns + "a")
        .SingleOrDefault(e => (string)e.Attribute("n") == "mobile");

    var address = (string)account.Elements(ns + "a")
        .SingleOrDefault(e => (string)e.Attribute("n") == "zimbraMailDeliveryAddress");
}

See a working demo using your file here: https://dotnetfiddle.net/UOV40v 在此处查看使用您的文件的有效演示: https : //dotnetfiddle.net/UOV40v

SelectSingleNode will only return a single node. SelectSingleNode将仅返回单个节点。 SelectNodes returns a list of nodes (XmlNodeList) and may be what you are looking for to get them all. SelectNodes返回节点列表(XmlNodeList),这可能是您寻找所有节点的目的。

Link to the specification of XmlNode.SelectNodes 链接到XmlNode.SelectNodes的规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM