如何在c#中按名称获取xml属性值

时间:2014-03-10 14:15:09

标签: c# xml xml-parsing linq-to-xml xmlreader

<Entity>
    <Fields>
        - <Field Name="expected">
          <Value>011919884841926</Value> 
          </Field>
        - <Field Name="name">
          <Value>Dial</Value>
     </Fields>
    <Fields>.....</Fields>
    <Fields>.....</Fields>
    <Fields>.....</Fields>
    <Fields>......</Fields>
    <Fields>....</Fields>
</Entity>

如何将Dial = 011919884841926作为c#中的键值对。

List<Dictionary<string, string>> tempDefect = new List<Dictionary<string, string>>();
                if (!string.IsNullOrEmpty(responseXML))
                {
                    XmlDocument configXml = new XmlDocument();
                    configXml.LoadXml(responseXML);
                    XPathNavigator navigator = configXml.CreateNavigator();
                    XPathNodeIterator nodeIterator = (XPathNodeIterator)navigator.Evaluate(rootEntities + "/" + rootEntity + "/" + rootElement);
                    for (int i = 0; i < nodeIterator.Count; i++)
                    {
                        nodeIterator.MoveNext();
                        Dictionary<string, string> tempDefectRows = new Dictionary<string, string>();
                        configXml = new XmlDocument();
                        configXml.LoadXml(nodeIterator.Current.OuterXml.ToString());
                        navigator = configXml.CreateNavigator();
                        XPathNodeIterator nodeIterator1 = (XPathNodeIterator)navigator.Evaluate(rootElement + "/" + loginParamsElement);

                        if (nodeIterator.Count != 0)
                        {
                            for (int j = 0; j < nodeIterator1.Count; j++)
                            {
                                nodeIterator1.MoveNext();
                                XmlNode node = ((IHasXmlNode)nodeIterator1.Current).GetNode();
                                if (attributes.Contains((T)Enum.Parse(typeof(T), node.Attributes["Name"].Value.ToString().Replace("-", "_"), true)))
                                {
                                    if (node.HasChildNodes)
                                    {
                                        configXml.LoadXml(node.InnerXml.ToString());
                                        XPathNavigator navigator2 = configXml.CreateNavigator();
                                        XPathNodeIterator nodeIterator2 = (XPathNodeIterator)navigator2.Evaluate("Value");

                                        if (nodeIterator2.Current.ToString().Trim() != StripTagsCharArray(nodeIterator2.Current.ToString().Replace("&nbsp;", "").Trim()).Trim())
                                            {
                                                tempDefectRows.Add(node.Attributes["Name"].Value.ToString(), StripTagsCharArray(nodeIterator2.Current.ToString().Replace("&nbsp;", "").Trim()).Trim());
                                            }
                                            else
                                            {
                                                tempDefectRows.Add(node.Attributes["Name"].Value.ToString(), nodeIterator2.Current.ToString().Trim());
                                            }
                                    }
                                    else
                                    {
                                        tempDefectRows.Add(node.Attributes["Name"].Value.ToString(), string.Empty);
                                    }
                                }
                            }
                            if (tempDefectRows.ContainsValue(filterBy))
                            {
                                testID = tempDefectRows["id"].ToString();
                                tempDefect.Add(tempDefectRows);
                                if (typeof(T).Name.ToString() == "Test")
                                    break;
                            }

                        }
                    }
                }

这是我的研究和工作。但我在2到3个循环之后得到了理想的输出......这就是我在这里发布的reasson ..... 有没有简单的方法来获得解决方案。

2 个答案:

答案 0 :(得分:1)

Linq to xml是一个很好的解析xml字符串的方法Code project 有很好的文章LINQ to XML。这可能会对你有所帮助

答案 1 :(得分:1)

使用Linq To Xml

var xDoc = XDocument.Load(filename);
var dict = xDoc.Descendants("Fields")
            .ToDictionary(f => GetValue(f, "name"), f => GetValue(f, "expected"));


string GetValue(XElement root, string attr)
{
    return root.Elements("Field")
                .First(a => a.Attribute("Name").Value == attr)
                .Element("Value").Value;
}