C# 从字符串 xml 中获取节点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16451884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:02:24  来源:igfitidea点击:

get node value from string xml

c#xmldocument

提问by foluis

I have this string XML

我有这个字符串 XML

string innerXml = @"<detail><WCFFaultExcepcion xmlns=""http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId><Message>índice fuera de los límites de la matriz.</Message></WCFFaultExcepcion></detail>";

This is the stringXML

这是 stringXML

<detail>
    <WCFFaultExcepcion xmlns="http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId>
        <Message>índice fuera de los límites de la matriz.</Message>
    </WCFFaultExcepcion>
</detail>

What I want is to get the value of the detail Tag, I'm trying with this example but all return null o cero count, could you help me?

我想要的是获取详细信息标签的值,我正在尝试使用此示例,但都返回 null o cero 计数,您能帮我吗?

 private static void Example()
        {
            string innerXml = @"<detail><WCFFaultExcepcion xmlns=""http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId><Message>índice fuera de los límites de la matriz.</Message></WCFFaultExcepcion></detail>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(innerXml);

            XmlNode node = (XmlNode)doc.DocumentElement;
            XmlNode optionalNode = node.SelectSingleNode("/detail/WCFFaultExcepcion");
            XmlNode optionalNode1 = node.SelectSingleNode("detail/WCFFaultExcepcion");
            XmlNode optionalNode2 = node.SelectSingleNode("/detail/WCFFaultExcepcion/ErrorId");
            XmlNode optionalNode3 = node.SelectSingleNode("detail/WCFFaultExcepcion/ErrorId");
            XmlElement optional = doc.SelectSingleNode(@"/detail/WCFFaultExcepcion/ErrorId") as XmlElement;
            XmlElement optiona2 = doc.SelectSingleNode(@"detail/WCFFaultExcepcion/ErrorId") as XmlElement;
            XmlNode xNode = doc.DocumentElement.SelectNodes("ErrorId")[0];
            XmlNodeList xnList = doc.SelectNodes("/detail/WCFFaultExcepcion");
            XmlNodeList xnList1 = doc.SelectNodes("detail/WCFFaultExcepcion");
            XmlNodeList xnList2 = doc.SelectNodes("/detail/WCFFaultExcepcion/ErrorId");
            XmlNodeList xnList3 = doc.SelectNodes("detail/WCFFaultExcepcion/ErrorId");
        }

采纳答案by Swen Kooij

I think this might be the solution for you:

我认为这可能是您的解决方案:

XmlDocument doc = new XmlDocument();
doc.LoadXml(innerXml);

XmlNodeList ErrorIdTags = doc.GetElementsByTagName("ErrorId");
if(ErrorIdTags.Count <= 1)
{
    // The tag could not be fond
}
else
{
    // The tag could be found!
    string ErrorId = ErrorIdTags[0].InnerText;
}

回答by nl-x

The first try XmlNode node = (XmlNode)doc.DocumentElement;should work and should contain children. Try XmlNode firstChildNode = node.FirstChild;... this will get you the first child, and won't be empty/null.

第一次尝试XmlNode node = (XmlNode)doc.DocumentElement;应该有效并且应该包含孩子。试试XmlNode firstChildNode = node.FirstChild;......这会让你得到第一个孩子,并且不会为空/空。

But when using xpath, you will get into problems because of the defined namespace. You will have to create a new namespace in doc, and assign http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidadesa shorthand (eg. sh) ... then later in your xpath you can like go doc.SelectSingleNode(@"/detail/sh:WCFFaultExcepcion/sh:ErrorId")

但是当使用 xpath 时,你会因为定义的命名空间而遇到问题。您必须在 doc 中创建一个新的命名空间,并分配http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades一个速记(例如 sh)...然后在您的 xpath 中您可以喜欢 godoc.SelectSingleNode(@"/detail/sh:WCFFaultExcepcion/sh:ErrorId")

回答by aquaraga

Try using Linq to Xml (http://msdn.microsoft.com/en-us/library/bb387098%28v=VS.100%29.aspx) - the code will be really elegant.

尝试使用 Linq to Xml ( http://msdn.microsoft.com/en-us/library/bb387098%28v=VS.100%29.aspx) - 代码会非常优雅。