简体   繁体   English

从 C# 中的大 xml 读取子节点值

[英]Reading a child node value from a big xml in C#

I have a 4000 lines of xmlresponse from a service.我有来自服务的 4000 行 xmlresponse。 I extracted the particular section from that big xml using required tag name我使用所需的标签名称从那个大 xml 中提取了特定部分

XmlNodeList xmlForms = xmlDoc.GetElementsByTagName("Form"); XmlNodeList xmlForms = xmlDoc.GetElementsByTagName("Form");

From this node sample partial xml looks like below从这个节点样本部分 xml 看起来像下面

 <Form>
  <FormID>3434294</FormID>
  <StatusDate>0001-01-01T00:00:00</StatusDate>
  <InternalFormNo>CADFADSFSAGDSADG01</InternalFormNo>
  <ExternalFormNo>CADFASDFASFSC1001</ExternalFormNo>
  <ProposalDescription>Treatment</ProposalDescription>
  <ProposalForm>false</ProposalForm>
  <StateApprovals>
    <StateApproval>
      <StateApprovalID>2245363363636</StateApprovalID>
      <IssueLimitSet>
        <IssueLimitSetID>88</IssueLimitSetID>
        <Name>AccAdv</Name>
        <Note />
        <ModifyDate>0001-01-01T00:00:00</ModifyDate>
        <IssueLimits>
          <IssueLimit>
            <IssueLimtID>80</IssueLimtID>
            <GIAmount>62</GIAmount>
            <SIAmount>0.00</SIAmount>
            <ChangeProcessingCGIAmount>0</ChangeProcessingCGIAmount>
            <MaximumMultiSelect>0</MaximumMultiSelect>
            <CreateDate>0001-01-01T00:00:00</CreateDate>
            <ModifyDate>0001-01-01T00:00:00</ModifyDate>
          </IssueLimit>
        </IssueLimits>
      </IssueLimitSet>
      <SpecialProcessing>false</SpecialProcessing>
      <ModifyUser>TESTUSER</ModifyUser>
      <ModifyDate>2016-02-17T17:52:59.163</ModifyDate>
    </StateApproval>
  </StateApprovals>
</Form>

I need to read the node GIAmount and extract it's value (62).我需要读取节点 GIAmount 并提取它的值 (62)。 But xpath notation always giving null value to me.但是 xpath 符号总是给我 null 值。 How to read this child node from this sub-xml.如何从此子 xml 中读取此子节点。 simple xpath also giving null always.简单的 xpath 也总是给 null。

foreach (XmlNode form in xmlForms)
                        {
                            var statusDate= form.SelectSingleNode("/Form/StatusDate"); //This always null i am getting

}

----------------Original xml------------Under Variations->Forms->Form --Some scenarios multiple form tags comes i need to read that particular node in every form------- ----------------原始 xml------------在 Variations->Forms->Form --一些场景下需要阅读多个表单标签每种形式的特定节点--------

<?xml version="1.0" encoding="utf-8"?>
<Product xmlns="http://testtest.com/twmku">
  <ProductID>72</ProductID>
  <InternalDescription>AccidentAdvance</InternalDescription>
  <ExternalDescription>AccidentAdvance</ExternalDescription>
  <Variations>
    <Variation>
      <VariationID>231</VariationID>
      <InternalDescription>AccidentAdvance123</InternalDescription>
      <ExternalDescription>AccidentAdvance</ExternalDescription>
      <ProposalDescription />
      <IsProposalReady>false</IsProposalReady>      
      <StatusDate>2009-03-26T00:00:00</StatusDate>
      <EffectiveDate>2009-04-01T00:00:00</EffectiveDate>
      <WithdrawnDate>0001-01-01T00:00:00</WithdrawnDate>
      <ModifyUser>Utesruser</ModifyUser>
      <ModifyDate>2011-11-30T10:35:26.313</ModifyDate>
      <Employers />
      <Forms>
        <Form>
          <FormID>3493</FormID>        
          <IsGeneric>true</IsGeneric>
          <HasLimitsInUnits>true</HasLimitsInUnits>
          <Description>AccAdv Master Policy</Description>
          <CreateUser>US\testMW</CreateUser>
          <CreateDate>0001-01-01T00:00:00</CreateDate>
          <ModifyUser>US\testMW</ModifyUser>
          <ModifyDate>2011-12-12T11:42:40.06</ModifyDate>          
          <DataElements />
          <StateApprovals>
            <StateApproval>
              <StateApprovalID>2256556</StateApprovalID> 
              <IssueLimitSet>
                <IssueLimitSetID>88</IssueLimitSetID>
                <Name>AccAdv Modtest</Name>
                <Note />
                <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                <IssueLimits>
                  <IssueLimit>
                    <IssueLimtID>80</IssueLimtID>
                    <Keyword>AccAdv Mod 1</Keyword>
                    <MinMarketingLimit>0.50</MinMarketingLimit>
                    <MaxMarketingLimit>12.00</MaxMarketingLimit>
                    <CGIAmount>0.00</CGIAmount>
                    <GIAmount>6</GIAmount>
                    <SIAmount>0.00</SIAmount>
                    <ChangeProcessingCGIAmount>0</ChangeProcessingCGIAmount>                   
                    <CreateDate>0001-01-01T00:00:00</CreateDate>
                    <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                  </IssueLimit>
                </IssueLimits>
              </IssueLimitSet>
              <StateRequirementSet />
              <QuestionSet />
              <SICSet />
              <DateFiled>0001-01-01T00:00:00</DateFiled>          
              <SpecialProcessing>false</SpecialProcessing>
              <ModifyUser>JBtestD</ModifyUser>
              <ModifyDate>2016-02-18T14:39:50.927</ModifyDate>
            </StateApproval>
          </StateApprovals>
          <Parameters />
          <IsSelected>true</IsSelected>
        </Form>

        <Form>
          <FormID>3495</FormID>
          <IsGeneric>true</IsGeneric>
          <HasLimitsInUnits>true</HasLimitsInUnits>
          <Description>AccAdv Master main</Description>
          <CreateUser>US\testMqW</CreateUser>
          <CreateDate>0001-01-01T00:00:00</CreateDate>
          <ModifyUser>US\testMW</ModifyUser>
          <ModifyDate>2011-12-12T11:42:40.06</ModifyDate>
          <DataElements />
          <StateApprovals>
            <StateApproval>
              <StateApprovalID>26556</StateApprovalID>
              <IssueLimitSet>
                <IssueLimitSetID>88</IssueLimitSetID>
                <Name>AccAdv Moretest</Name>
                <Note />
                <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                <IssueLimits>
                  <IssueLimit>
                    <IssueLimtID>84</IssueLimtID>
                    <Keyword>AccAdv Mod 1</Keyword>
                    <MinMarketingLimit>0.50</MinMarketingLimit>
                    <MaxMarketingLimit>12.00</MaxMarketingLimit>
                    <CGIAmount>0.00</CGIAmount>
                    <GIAmount>34</GIAmount>
                    <SIAmount>0.00</SIAmount>
                    <ChangeProcessingCGIAmount>0</ChangeProcessingCGIAmount>
                    <CreateDate>0001-01-01T00:00:00</CreateDate>
                    <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                  </IssueLimit>
                </IssueLimits>
              </IssueLimitSet>
              <StateRequirementSet />
              <QuestionSet />
              <SICSet />
              <DateFiled>0001-01-01T00:00:00</DateFiled>
              <SpecialProcessing>false</SpecialProcessing>
              <ModifyUser>JBtestD</ModifyUser>
              <ModifyDate>2016-02-18T14:39:50.927</ModifyDate>
            </StateApproval>
          </StateApprovals>
          <Parameters />
          <IsSelected>true</IsSelected>
        </Form>
      </Forms>      
      <ParameterValueSets />
      <AllowCustomRates>false</AllowCustomRates>
    </Variation>
  </Variations>
</Product>

You are already targeting the Form element, thus there's no reason to include it in the XPath.您已经以Form元素为目标,因此没有理由将其包含在 XPath 中。 Simply skip that tag, and compose the path with its descendants.只需跳过该标签,并用它的后代组成路径。

In your case: "StateApprovals/StateApproval/IssueLimitSet/IssueLimits/GIAmount"在您的情况下: "StateApprovals/StateApproval/IssueLimitSet/IssueLimits/GIAmount"

Or, more compact: "//GIAmount"或者,更简洁: "//GIAmount"

Note that my answer is based on the fragment of your post, and the path may change on the actual content.请注意,我的回答是基于您帖子的片段,路径可能会根据实际内容而改变。

It is better to use LINQ to XML .最好使用LINQ 到 XML Using its methods, it is very easy to get to any XML element.使用它的方法,很容易得到任何 XML 元素。

(1) Your XML has a default namespace. (1) 您的 XML 具有默认命名空间。 It needs to be taken into account.需要考虑到这一点。 (2) Multiple <Form>...</Form> elements require a loop. (2) 多个<Form>...</Form>元素需要一个循环。

c# c#

void Main()
{
    XElement xelem = XElement.Parse(@"<Product xmlns='http://testtest.com/twmku'>
  <ProductID>72</ProductID>
  <InternalDescription>AccidentAdvance</InternalDescription>
  <ExternalDescription>AccidentAdvance</ExternalDescription>
  <Variations>
    <Variation>
      <VariationID>231</VariationID>
      <InternalDescription>AccidentAdvance123</InternalDescription>
      <ExternalDescription>AccidentAdvance</ExternalDescription>
      <ProposalDescription />
      <IsProposalReady>false</IsProposalReady>      
      <StatusDate>2009-03-26T00:00:00</StatusDate>
      <EffectiveDate>2009-04-01T00:00:00</EffectiveDate>
      <WithdrawnDate>0001-01-01T00:00:00</WithdrawnDate>
      <ModifyUser>Utesruser</ModifyUser>
      <ModifyDate>2011-11-30T10:35:26.313</ModifyDate>
      <Employers />
      <Forms>
        <Form>
          <FormID>3493</FormID>        
          <IsGeneric>true</IsGeneric>
          <HasLimitsInUnits>true</HasLimitsInUnits>
          <Description>AccAdv Master Policy</Description>
          <CreateUser>US\testMW</CreateUser>
          <CreateDate>0001-01-01T00:00:00</CreateDate>
          <ModifyUser>US\testMW</ModifyUser>
          <ModifyDate>2011-12-12T11:42:40.06</ModifyDate>          
          <DataElements />
          <StateApprovals>
            <StateApproval>
              <StateApprovalID>2256556</StateApprovalID> 
              <IssueLimitSet>
                <IssueLimitSetID>88</IssueLimitSetID>
                <Name>AccAdv Modtest</Name>
                <Note />
                <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                <IssueLimits>
                  <IssueLimit>
                    <IssueLimtID>80</IssueLimtID>
                    <Keyword>AccAdv Mod 1</Keyword>
                    <MinMarketingLimit>0.50</MinMarketingLimit>
                    <MaxMarketingLimit>12.00</MaxMarketingLimit>
                    <CGIAmount>0.00</CGIAmount>
                    <GIAmount>6</GIAmount>
                    <SIAmount>0.00</SIAmount>
                    <ChangeProcessingCGIAmount>0</ChangeProcessingCGIAmount>                   
                    <CreateDate>0001-01-01T00:00:00</CreateDate>
                    <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                  </IssueLimit>
                </IssueLimits>
              </IssueLimitSet>
              <StateRequirementSet />
              <QuestionSet />
              <SICSet />
              <DateFiled>0001-01-01T00:00:00</DateFiled>          
              <SpecialProcessing>false</SpecialProcessing>
              <ModifyUser>JBtestD</ModifyUser>
              <ModifyDate>2016-02-18T14:39:50.927</ModifyDate>
            </StateApproval>
          </StateApprovals>
          <Parameters />
          <IsSelected>true</IsSelected>
        </Form>
        <Form>
          <FormID>3495</FormID>
          <IsGeneric>true</IsGeneric>
          <HasLimitsInUnits>true</HasLimitsInUnits>
          <Description>AccAdv Master main</Description>
          <CreateUser>US\testMqW</CreateUser>
          <CreateDate>0001-01-01T00:00:00</CreateDate>
          <ModifyUser>US\testMW</ModifyUser>
          <ModifyDate>2011-12-12T11:42:40.06</ModifyDate>
          <DataElements />
          <StateApprovals>
            <StateApproval>
              <StateApprovalID>26556</StateApprovalID>
              <IssueLimitSet>
                <IssueLimitSetID>88</IssueLimitSetID>
                <Name>AccAdv Moretest</Name>
                <Note />
                <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                <IssueLimits>
                  <IssueLimit>
                    <IssueLimtID>84</IssueLimtID>
                    <Keyword>AccAdv Mod 1</Keyword>
                    <MinMarketingLimit>0.50</MinMarketingLimit>
                    <MaxMarketingLimit>12.00</MaxMarketingLimit>
                    <CGIAmount>0.00</CGIAmount>
                    <GIAmount>34</GIAmount>
                    <SIAmount>0.00</SIAmount>
                    <ChangeProcessingCGIAmount>0</ChangeProcessingCGIAmount>
                    <CreateDate>0001-01-01T00:00:00</CreateDate>
                    <ModifyDate>0001-01-01T00:00:00</ModifyDate>
                  </IssueLimit>
                </IssueLimits>
              </IssueLimitSet>
              <StateRequirementSet />
              <QuestionSet />
              <SICSet />
              <DateFiled>0001-01-01T00:00:00</DateFiled>
              <SpecialProcessing>false</SpecialProcessing>
              <ModifyUser>JBtestD</ModifyUser>
              <ModifyDate>2016-02-18T14:39:50.927</ModifyDate>
            </StateApproval>
          </StateApprovals>
          <Parameters />
          <IsSelected>true</IsSelected>
        </Form>
      </Forms>      
      <ParameterValueSets />
      <AllowCustomRates>false</AllowCustomRates>
    </Variation>
  </Variations>
</Product>");

    //string GIAmount = GIAmount.Descendants(ns + "GIAmount").FirstOrDefault().Value;
    XNamespace ns = xelem.GetDefaultNamespace();

    foreach (var el in xelem.Descendants(ns + "GIAmount"))
    {
        Console.WriteLine("GIAmount={0}", el.Value);    
    }
}

Output Output

GIAmount=6
GIAmount=34

One value is usually not enough information.一个值通常是不够的信息。 Try xml linq like below:尝试 xml linq 如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            List<StateApproval> approvals = doc.Descendants(ns + "StateApproval").Select(x => new StateApproval()
            {
                id = (string)x.Element(ns + "StateApprovalID"),
                limitSetId = (string)x.Descendants(ns + "IssueLimitSetID").FirstOrDefault(),
                name = (string)x.Descendants(ns + "Name").FirstOrDefault(),
                modifyDate = (DateTime)x.Descendants(ns + "ModifyDate").FirstOrDefault(),
                issueLimitId = (string)x.Descendants(ns + "IssueLimtID").FirstOrDefault(),
                giAmount = (decimal)x.Descendants(ns + "GIAmount").FirstOrDefault(),
                siAmount = (decimal)x.Descendants(ns + "SIAmount").FirstOrDefault(),
                changeAmount = (decimal)x.Descendants(ns + "ChangeProcessingCGIAmount").FirstOrDefault(),
                min = (decimal)x.Descendants(ns + "MinMarketingLimit").FirstOrDefault(),
                max = (decimal)x.Descendants(ns + "MaxMarketingLimit").FirstOrDefault(),
                createDate = (DateTime)x.Descendants(ns + "CreateDate").FirstOrDefault(),
            }).ToList();
        }
    }
    public class StateApproval
    {
        public string id { get; set; }
        public string limitSetId { get; set; }
        public string name { get; set; }
        public DateTime modifyDate { get; set; }
        public string issueLimitId { get; set; }
        public decimal giAmount { get; set; }
        public decimal siAmount { get; set; }
        public decimal changeAmount { get; set; }
        public decimal min { get; set; }
        public decimal max { get; set; }
        public DateTime createDate { get; set; }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM