gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:19:50 24 4
gpt4 key购买 nike

我有这个字符串 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 标签的值,我正在尝试这个例子,但所有返回 null o cero count,你能帮我吗?

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

关于c# - 从字符串 xml 中获取节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16451884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com