XDocument.parse将字符串作为一个元素进行解析。

问题描述 投票:0回答:1

当我使用XDocument.parse时,它将整个字符串解析为一个元素,解析后的文档没有任何结构,我正在使用regex来查找我需要工作的XML字符串。

int total=1;
                List<XDocument> documents = new List<XDocument>();

                Match collection = Regex.Match(innerXMl, @"<claimDetails" + total + @">[\s\S]+          
                <\/claimDetails" + total + ">");

                Regex regex = new Regex(@"<[\/]{0,1}?claimDetails"+total+">");

                System.Xml.XmlDocument document = new System.Xml.XmlDocument();

                if (collection.Success)
                {

                    string value = collection.Value;

价值 value 变量是

<claimDetails1>
  <regNo1>DL12SG7754</regNo1>
  <chasisNo1>ME4JF505EG7144924</chasisNo1>
  <engineNo1>JF50E73144192</engineNo1>
  <insurerName1>Shriram General Insurance Co. Ltd.</insurerName1>
  <typeOfClaim1>OD</typeOfClaim1>
  <dateOfLoss1>2020-02-06</dateOfLoss1>
  <claimIntimationDate1>2020-02-12</claimIntimationDate1>
  <ODClaimsPaid1>8954</ODClaimsPaid1>
  <whetherTotalLossClaim1>No</whetherTotalLossClaim1>
  <whetherTheftClaim1>No</whetherTheftClaim1>
  <totalTPClaimsPaid1>0.0</totalTPClaimsPaid1>
  <tpOpenClaimProvison1>0</tpOpenClaimProvison1>
  <tpCloseClaimProvison1>0</tpCloseClaimProvison1>
  <expensesPaid1>0</expensesPaid1>
  <claimstatus1>Claim Fully settled</claimstatus1>
  <searchBasedOn1>REG_NUM</searchBasedOn1>
</claimDetails1>

我想把它解析成Xdocument。


                    documents.Add(XDocument.Parse(value));
                }

上面一行将值变量解析成Xdocument对象,没有错误,但整个文档只有一个元素,那就是 "Xdocument"。claimDetails1innerXml as entire thing in between.请帮助我。

c# asp.net linq-to-xml
1个回答
0
投票

XDocument.Parse从你传递的字符串创建一个XDocument。然后你使用XDocument来反序列化XML以获得所需的值。

请看一下 https:/docs.microsoft.comen-usdotnetapisystem.xml.linq.xdocument.parse?view=netcore-3.1。

你的问题不是很清楚,也不知道你说的 "没有结构 "是什么意思(即是一个坏的xml没有解析还是只是随机结构的数据),所以如果不是你要找的,我会更新我的回答。

List<XElement> documents = new List<XElement>();
XDocument xdoc = XDocument.Parse(EntireXMLstring);
if (xdoc.Descendants("claimDetails1").Any())
{
    IEnumerable<XElement> requiredNode = xDoc.Descendants("claimDetails");
    documents.AddRange(requiredNode);
}
© www.soinside.com 2019 - 2024. All rights reserved.