简体   繁体   English

XML节点名称和属性不可用

[英]XML node name and attributes are not available

I have the following XML structure: 我具有以下XML结构:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<values>
    <bool key="Chapter_1.Boolean1.value">true</bool>
    <string key="Chapter_1.Text1.value">abc</string>
    <string key="Chapter_1.Text2.value">Inspection done (2)</string>
    <number key="Chapter_1.Number1.value">128</number>
    <number key="Chapter_1.Number2.value">34539718</number>
    <number key="Chapter_1.Number3.value">3</number>
    <datetime key="Chapter_2.Chapter_2_1.DateTime1.value">2020-06-02T09:00:00+03:00</datetime>
    <datetime key="Chapter_2.Chapter_2_1.DateTime2.value">2016-02-05T00:00:00+02:00</datetime>
    <string key="Chapter_3.Text4.value">52</string>
    <string key="Chapter_3.Text5.value">22</string>
    <number key="Chapter_3.Number6.value">34539718</number>
</values>

and the following C# code: 和以下C#代码:

var settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;

using (var xmlReader = new XmlTextReader(xmlFilePath))
{
    while (xmlReader.Read())
    {
        var nodeName = xmlReader.Name;
        var attrName = xmlReader.GetAttribute("key");
    }
}

The problem is that the node name is empty and there are no attributes for the following keys: 问题在于节点名称为空,并且以下键没有属性:

  • Chapter_1.Text1.value Chapter_1.Text1.value
  • Chapter_1.Number1.value Chapter_1.Number1.value
  • Chapter_3.Text5.value Chapter_3.Text5.value

Anyone has any idea what could be the problem? 任何人都知道可能是什么问题?

It would be easier to use Xml to Linq : 使用Xml到Linq会更容易:

var xml = XDocument.Load(__PATH_TO_XML__);
var values = xml.XPathSelectElements("/values/*")
    .Select(x => new
    {
        Type = x.Name,
        Key = x.Attribute("key"),
        Value = x.Value
    });

The code worked well here, I was able to access all xml tags and attributes. 该代码在这里运行良好,我能够访问所有xml标记和属性。

Maybe you're confused because at each xmlReader.Read() it reads only one part of the tag. 也许您很困惑,因为在每个xmlReader.Read()它仅读取标记的一部分。 So to read all the tag with key " Chapter_1.Text1.value ", first it reads a tag with name string and key " Chapter_1.Text1.value " , then it reads something without a name, without a attribute, but with value "abc" and then it reads the tag closing with name string , but no attribute and no value. 因此,要读取键为“ Chapter_1.Text1.value ”的所有标签, 首先读取名称为string和键为“ Chapter_1.Text1.value的标签,然后读取没有名称,没有属性但值为“”的内容。 abc” ,然后读取名称为string的标签,但没有属性,也没有值。

If you want to read the value as well, try this 如果您也想读取该值,请尝试以下操作

      using (var xmlReader = new XmlTextReader(@"yourxmlfile"))
        {
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {

                    var nodeName = xmlReader.Name;
                    var attrName = xmlReader.GetAttribute("key");

                    Console.WriteLine(nodeName);
                    Console.WriteLine(attrName);

                }
                if (xmlReader.NodeType==XmlNodeType.Text)
                {
                    Console.WriteLine(xmlReader.Value);
                }
            }
        }

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

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