我有这个字符串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>";

这是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>

我想要的是得到detail标记的值,我正在尝试这个例子,但是都返回空的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");
        }

最佳答案

我想这可能是你的解决方案:

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;
}

07-27 18:28