繁体   English   中英

如何读取XML文件节点属性和子节点属性值?

[英]How can I read the XML file Node attributes and Child node attributes values?

如何读取包含内容的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<A>
   <B value="1">
     <Hash algo="SHA256" value="905C45B51B970434D7159641D9F6A88DC91E9C35030618A729C8E4BE174711AF" />
   </B>
   <B value="2">
     <Hash algo="SHA256" value="649721FF455E9B100E691A3857696350E14364029C34C9438AB3EA9665C91292" />
   </B>
   <B value="3">
     <Hash algo="SHA256" value="90FC91C4B82BF440FDAFECF3303DCA8FB9F2E9D7EFFAE394D8B74D0C7CD7DA10" />
   </B>
</A>

在上面的xml文件中,我想读取所有B标签的“value”属性值和标签Hash “algorithm”或“value”属性值的值。

这是我正在使用的代码:

        var settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        using (var stm = new FileStream(@"xmlFilePath", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (var reader = XmlReader.Create(stm, settings))
        {
            while (reader.Read())
            {
                string srcFileHash=null;
                reader.ReadToDescendant("B");
                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "B")
                {
                    reader.MoveToAttribute("value");
                    var bValue=reader.Value;   // get the B tag attribute value.

                    //also want to read the <hash value=? and algo=?. but I dnt know how to get these hash tag attributes.
                }
            }
        }
XDocument xDoc = XDocument.Parse(xml);
var result = xDoc.Descendants("B")
    .Select(b => new
    {
        BValue = b.Attribute("value").Value,
        Alg = b.Element("Hash").Attribute("algo").Value,
        AlgValue = b.Element("Hash").Attribute("value").Value,
    })
    .ToArray();
XElement doc = XElement.Load(yourxmlfilename);//Load the document

foreach (XElement b in doc.Elements("B"))//Iterate all elements B
{
    var xAttribute = b.Attribute("value");//get attribute called value
    if (xAttribute != null)
    {
        string v = xAttribute.Value;//now you have the B value
    }

    foreach (XElement h in b.Elements("Hash"))//Iterate all elements Hash
    {
        var xAttributev = h.Attribute("value");//get attribute called value
        if (xAttributev != null)
        {
               string hashValue = xAttribute.Value;//now you have the Hash value
        }

        var xAttributeh = h.Attribute("algo");//get attribute called algo
        if (xAttributeh != null)
        {
           string algorithm = xAttributeh.Value;//now you have the Hash algorithm
        }
    }
}

你的问题不清楚,但这里有一些快速的代码可以做你想要的。

XDocument xdoc = XDocument.Load(@"file.xml");
if (xdoc.Root != null)
{
    var bs = xdoc.Root.Descendants("B");

    foreach (var bNode in bs)
    {
        Console.Out.WriteLine("B Value: " + bNode.FirstAttribute.Value);
        var hash = bNode.XPathSelectElement("Hash");
        Console.Out.WriteLine("algo is : " + hash.FirstAttribute.Value);
        Console.Out.WriteLine("value is : " + hash.FirstAttribute.NextAttribute.Value);
    } 
}

我建议刷新XDocument和XmlDocument(两者仍然相关,具体取决于你的代码有多大:)

暂无
暂无

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

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