简体   繁体   English

ASP.NET Core 6 MVC:读取 xml 读取 outerxml 删除标签

[英]ASP.NET Core 6 MVC : read xml read outerxml remove tags

I am writing an ASP.NET Core 6 MVC app, and I have to read an XML with this format:我正在编写一个 ASP.NET Core 6 MVC 应用程序,并且我必须使用以下格式读取一个 XML:

<?xml version="1.0" encoding="utf-8" ?>
<taglines common-copyright="©2007 ">
    <tagline brandID="1" brandName="BCBS" >
        <registered registered="®Registered">
        </registered>
        <copyright copyright="©2007">
        </copyright>
        <tagline2 tagline2="Data.">
        </tagline2>
    </tagline>
</taglines>

I need to read the content in tag tagline2我需要阅读标签tagline2中的内容

The way I am doing it is我这样做的方式是

private  string ReadXMLAnthemFile()
{
    string footer = "";

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(String.Concat(this.Environment.WebRootPath,"/img/TagLines.xml"));

    foreach (XmlNode node in xmlDocument.SelectNodes("taglines/tagline/tagline2"))
    {
        footer = node.OuterXml;
    }

    return footer;
}

But node.OuterXml returns the entire tag但是node.OuterXml返回整个标签

<tagline2 tagline2="Data."></tagline2>

How can I get only the data inside the tag?如何只获取标签内的数据?

Thanks谢谢

You don't need to read the "outer" XML - you need to reach into the XML node you've fetched, and read it's tagline2 attribute - if present.您不需要读取“外部”XML - 您需要进入您已获取的 XML 节点,并读取它的tagline2属性 - 如果存在。

Try something like this:尝试这样的事情:

foreach (XmlNode node in xmlDocument.SelectNodes("taglines/tagline/tagline2"))
{
    if (node.Attributes["@tagline2"] != null)
    {
        footer = node.Attributes["@tagline2"].Value;
    }
}

That should get you the .Value of the tagline2 attribute of the XML node that your XPath expression matches - the value would be Data.那应该让你得到你的 XPath 表达式匹配的 XML 节点的tagline2属性的.Value - 该值将是Data. in your case here.在你这里的情况。

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

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