Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用xmlreader读取此xml_C#_Xml_Xmlreader - Fatal编程技术网

C# 如何使用xmlreader读取此xml

C# 如何使用xmlreader读取此xml,c#,xml,xmlreader,C#,Xml,Xmlreader,我的xml部分如下所示: <Note> <SpecialText att1="" /> </Note> 但是,如果便条下的内容只是文字,我如何阅读内容呢。 如果我使用reader.ReadInnerXml,它将读取所有内容,因此我没有机会查看它是SpecialText XmlElement还是纯文本 非常感谢如果您使用XElement.Loadfile,那么您可以使用 XElement xfile = XElement.Load(file); XElemen

我的xml部分如下所示:

<Note>
<SpecialText att1="" />
</Note>
但是,如果便条下的内容只是文字,我如何阅读内容呢。 如果我使用reader.ReadInnerXml,它将读取所有内容,因此我没有机会查看它是SpecialText XmlElement还是纯文本

非常感谢

如果您使用XElement.Loadfile,那么您可以使用

XElement xfile = XElement.Load(file);
XElement note = xfile.Path("path/to/note");
if(note.HasElements) 
    // read the element
else 
    string text = (string)note;
注意:此处获取路径:

如果使用XElement.Loadfile,则可以使用

XElement xfile = XElement.Load(file);
XElement note = xfile.Path("path/to/note");
if(note.HasElements) 
    // read the element
else 
    string text = (string)note;

注意:在这里获取路径:

现在这听起来可能有点屈尊俯就,但我认为通过挖掘参考资料,这是很容易回答的问题。那么我可能还是不完全理解你的问题。如果下面的答案不是你想要的,只需发布更多细节,我很乐意帮助你

要确定内容是否只是文本,只需检查内容,然后随意使用:

while (reader.Read())             
    {             
    if (reader.NodeType == XmlNodeType.Element)             
    {             
        switch (reader.LocalName.ToLower())             
        {             
            case MMLElement.SpecialText:             
            //// read related attributes             
            break;             
        }             
    }             

    else if (reader.NodeType == XmlNodeType.Text) 
    { 
      string thisIsjustText = reader.value;
    }
    //whatever comes next
}

这听起来可能有点屈尊俯就,但我认为这很容易通过挖掘参考资料来回答。那么我可能还是不完全理解你的问题。如果下面的答案不是你想要的,只需发布更多细节,我很乐意帮助你

要确定内容是否只是文本,只需检查内容,然后随意使用:

while (reader.Read())             
    {             
    if (reader.NodeType == XmlNodeType.Element)             
    {             
        switch (reader.LocalName.ToLower())             
        {             
            case MMLElement.SpecialText:             
            //// read related attributes             
            break;             
        }             
    }             

    else if (reader.NodeType == XmlNodeType.Text) 
    { 
      string thisIsjustText = reader.value;
    }
    //whatever comes next
}