簡體   English   中英

解析 CDATA xml

[英]parse CDATA xml

我如何在 CDATA 中獲得價值?

我所擁有的:它是字符串我想解析它並獲得像 NOM_VAG 和其他的值

<?xml version="1.0" encoding="windows-1251"?>
<GetInformReply><Inform_ID>-14</Inform_ID><ASOUPReply>
<![CDATA[<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getReferenceSPV4664Response xmlns:ns2="http://service.siw.pktbcki.rzd/">
<return><referenceSPV4664><row><NOM_VAG>75142901</NOM_VAG><ROD_VAG_UCH>70</ROD_VAG_UCH><KOD_SOB>20</KOD_SOB><DATE_NACH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><STAN_NACH>0</STAN_NACH><DOR_NACH>0</DOR_NACH><STR_NACH>0</STR_NACH><DATE_KON xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><STAN_NAZN>612003</STAN_NAZN><DOR_NAZN>61</DOR_NAZN><STR_NAZN>643</STR_NAZN><GRUZPOL>4232</GRUZPOL><GRUZPOL_OKPO>0000000000</GRUZPOL_OKPO>
</row></referenceSPV4664><amount>1</amount><amountRF>1</amountRF><amountNotRF>0</amountNotRF><codeTypeObject>1</codeTypeObject><returnCode>0</returnCode><errorCode>0</errorCode><errorMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><title>Справка о дислокации вагона</title><dateIzm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/></return></ns2:getReferenceSPV4664Response></S:Body>
</S:Envelope>
]]>
 </ASOUPReply></GetInformReply>

我需要在 CDATA 之間解析 xml

What I did:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlText);
XmlElement xRoot = xDoc.DocumentElement;
xRoot.GetElementsByTagName("<![CDATA[]]>");
foreach (XmlNode item in xRoot)
    {
        if (item.LocalName == "ASOUPReply")
    {
        XmlNode attr = item.Attributes.GetNamedItem("row");
        foreach (var itemXMl in ((System.Xml.XmlCharacterData)item.LastChild).Data)
        {
                            
        }
return xmlText; 

UPD:這有效

public void pp (string text)
        {
            XDocument xml = XDocument.Parse(text);
            var soapResponse = xml.Descendants().Where(x => x.Name.LocalName == "row").Select(x => new SPR4664()
            {
                NOM_VAG = (string)x.Element(x.Name.Namespace + "NOM_VAG"),
                ROD_VAG_UCH = (string)x.Element(x.Name.Namespace + "ROD_VAG_UCH"),
            }).FirstOrDefault();
        }

模型:

public class SPR4664
    {
        public string NOM_VAG { get; set; }
        public string ROD_VAG_UCH { get; set; }

    }

但我有大約 30 行。 我必須手動寫每個人嗎? 關聯

選擇ASOUPReply元素節點,讀出其InnerText ,將其提供給第二個XmlDocumentLoadXml方法(您可能需要消除 XML 聲明之前的任何空格),然后選擇NOM_VAG元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM