使用 Xpath/HtmlAgilityPack 在页面长度的四分之一处插入标记...程序挂起

Using Xpath/HtmlAgilityPack to insert markup a quarter of the length down the page...program is hanging

我正在使用 HtmlAgilityPack 获取页面长度四分之一的字数,使用 asp.net Core MVC 查看从控制器返回的字 lengths/urls,然后选择该元素是附加一个 hr 标签(只是一个占位符,用于稍后的代码)。当我从另一个方法获得中间点并完美附加时,代码工作正常,但由于某种原因,获取四分之一长度元素的代码只会使程序挂起,即使我有第一季度节点的索引.


private static HtmlWeb web = new HtmlWeb();
private static HtmlDocument doc = web.Load('specific url address');
HtmlNode hr = doc.CreateElement("hr");

var quarterIndex = FirstQuarterTag(); //This returns an int of 5 to be passed in the line below
var xPathString = string.Format("//main[@id='main2']//div[@class='text-block']/*[{0}]", quarterIndex); //This returns the Xpath string "//main[@id='main2']//div[@class='text-block']/*[5]"

这是导致程序挂起的行:

doc.DocumentNode.SelectSingleNode(xPathString).PrependChild(hr);

我在选择实际上不存在的节点时遇到了错误(它返回了 undefined 等),但这并没有引发错误,程序只是无限期地挂起。我会假设它只会在给定的 xpathstring 之前插入一个 hr 元素(当我找到中点与第一季度标记时,它起作用了。)此时我只是在旋转轮子,所以非常感谢任何帮助.如果需要更多信息,请告诉我。

编辑:这是 div 元素的标记,class 为 text-block:

        <div class="text-block">
            <h2 class="h3"></h2>
            <p>&nbsp;</p>
            <p>Big increases in sales can actually stunt business growth when a B2B company isn't prepared to meet the challenges in their online ecommerce operation such as making manual re-entries into back-office software, matching store data and inventory data and troubleshooting slowdowns and errors that create customer dissatisfaction. Manual entry and processes are just too antiquated, and even ERP and CRM software can't do the job properly unless the online store and back-office software communicate seamlessly and bidirectionally.</p>
            <p>The 
                <a href="/ecommerce-integrations/erp/oracle-integration">benefits of an ecommerce &amp; Oracle ERP integration</a> include the automation of most business processes. Data is validated outside the firewall before being accepted into the system, but this is accomplished in microseconds. Security protection is stronger while speeding the real-time dissemination of critical information and intelligence.</p>
            <p>
                <strong>Internal Management Benefits of integration with Oracle E-Business Suite</strong>
            </p>
            <p>Oracle E-Business Suite, or EBS, is a group of software applications that work with Oracle's acclaimed database software. The benefits of 
                <a href="/ecommerce-integrations/erp/oracle-integration">integration with Oracle E-Business Suite</a> include an 
                <a href="/ecommerce-integrations/erp/oracle-integration">automated point-to-point integration of Oracle CRM and ERP Platforms</a>. The E-Business Suite includes a range of application modules for different business needs including various aspects of CRM and ERP for wholesalers, retail sellers, distributors and manufacturers. Customer can get what they need as Software-as-a-Service, or SaaS, applications. [1]</p>
            <p>The benefits of integrating Oracle software applications for internal management include the following:</p>
            <p>
                <strong>Catalog Management</strong>
            </p>
            <ul>
                <li>The most critical 
                    <a href="/ecommerce-integrations/erp/oracle-integration">benefit of an Oracle ecommerce integration</a> is catalog management. B2B companies often deal with thousands of SKUs, and updating catalogs and prices becomes a nightmare without integration, which updates the information everywhere that the prices and content appear such as marketplace platforms, marketing communications, back-office accounting applications, social media pages and customer-specific pricing and pricing tiers.</li>
            </ul>
            <p>
                <strong>Supply Chain Management</strong>
            </p>
            <ul>
                <li>Oracle SCM Cloud offers advanced machine learning capabilities for better supply chain management. Companies can plan supply chain operations based on their evolving business needs, BI forecasts and demand-driven market trends. AI applications can gather information from thousands of sources to predict supply chain logjams, price increases and changes in supply and demand.</li>
            </ul>
            <p>
                <strong>Inventory Management</strong>
            </p>
            <ul>
                <li>Companies with fully integrated systems get a transparent view of all operations and warehouses in real-time to manage their inventories more strategically. Just-in-time inventory replenishment can save companies money, reduce dead inventory and ensure that stock levels match product demand.</li>
            </ul>
            <p>
                <strong>Customer Service</strong>
            </p>
            <ul>
                <li>Companies can offer their customers extended ways to communicate and request customer service in an integrated system. These methods include operating integrated customer service sites on social media, sending customers to external call centers, monitoring online chatter and reviews to address complaints and issues, providing FAQs and self-service features and offering live chat and chatbots to answer questions instantly.</li>
            </ul>
            <p>
                <strong>The User Experience</strong>
            </p>
            <p>The user experience, or UX, is critical in today's marketing and sales success. 
                <a href="/ecommerce-integrations/erp/oracle-integration">Oracle ecommerce integratio</a>n provides the following self-service capabilities that deliver outstanding UXs to each site visitor:</p>
            <ul>
                <li>Ability to access past invoices and buying history 24/7/365</li>
                <li>Onsite shipping integrations so that customers can choose from faster or cheaper shipping for each order</li>
                <li>Self-service options to request price quotes, product personalization and even custom products</li>
                <li>Automated tax and duty calculations for global sales</li>
                <li>Real-time currency conversions</li>
                <li>Access to product research resources</li>
                <li>Transparent information about the supply chain</li>
                <li>Drop shipping and split shipping available through self-service</li>
                <li>Automated fulfillment, product returns and delivery monitoring</li>
                <li>Extended payment options for customers</li>
            </ul>
            <p>An example of the benefits of extended payment options is that customers tend to buy where it's easy for them to pay. about 9 percent of retailers now offer Amazon Pay and 58 percent offer PayPal. PayPal transactions enjoy a 70 percent higher conversion rate than other payment methods. [2] Digital wallets, banks transfers, third-party payment services, internal credit accounts, POs and traditional credit and debit cards are used by B2B buyers, so accepting these payments is essential for increasing conversion rates. Clarity ecommerce can offer all these and more, helping to improve conversions by making purchasing easier for online shoppers.</p>
            <p>
                <strong>Understanding integration</strong>
            </p>
            <p>Although Oracle is a powerful system for managing online stores, most companies can't get the full benefits of integration out of the box unless they're building their platforms from scratch. There are always existing legacy systems, customer apps and other SaaS systems that won't connect without a custom integration. Some hardware and systems won't work because they're older models, but replacing major infrastructure could be extremely cost-prohibitive.</p>
            <p>Many companies don't choose Oracle Commerce as their sales platform for various reasons such as wanting to keep their existing software or needing access to the specialized features that other ecommerce platforms offer. Oracle software can be integrated with other ecommerce platforms--such as Clarity ecommerce--through a custom integration.</p>
            <p>
                <strong>Your Custom integration</strong>
            </p>
            <p>It's important to assess just how much technology you need before making any technology expansion. What existing systems do you want to preserve, and what level or technology can you afford? Working with an experienced B2B developer can solve many problems and manage your custom integration efficiently and less expensively. Clarity has 350 combined years of experience collaborating with major B2B platforms and companies that want to expand their businesses, and it has performed more than 3,000 integrations. The company works well with your IT staff, provides training, listens to your needs and specializes in B2B ecommerce and CRM-ERP integration development.</p>
            <p>
                <em>References:</em>
            </p>
            <p>[1] Searchoracle.techtarget.com: Oracle E-Business Suite</p>
            <p>
                <u>
                    <a href="https://searchoracle.techtarget.com/definition/Oracle-E-Business-Suite">https://searchoracle.techtarget.com/definition/Oracle-E-Business-Suite</a>
                </u>
            </p>
            <p>[2] 3dcart.com: Commerce statistics</p>
            <p>
                <u>
                    <a href="https://www.3dcart.com/ecommerce-market-statistics.html">https://www.3dcart.com/ecommerce-market-statistics.html</a>
                </u>
            </p>
            <article class="icon-box col-12 text-center py-7">
                <div class="icon-holder border border-dark flex-grow-1 rounded p-4 p-xl-6">
                    <header class="title-area">
                        <i class="icon d-flex align-items-center justify-content-center border border-dark bg-lightest">
                            <img src="/images/ico-guide.svg" alt="icon description" width="42" height="40">
                        </i>
                        <h3>Find out more</h3>
                        <p>Click here to review options to gather more info. From resource guides to complimentary expert review... we're here to help!</p>
                    </header>
                    <div class="btn-block">
                        <a href="#quote" class="btn btn-default">Get More Info</a>
                    </div>
                </div>
            </article>
        </div>
        
        

我不明白你为什么有问题,这是我做过的测试:

        var doc = new HtmlDocument();
        doc.Load(@"you url", Encoding.UTF8);
        var node = doc.DocumentNode.SelectSingleNode("//main[@id='main2']//div[@class='text-block']/*[5]");
        HtmlNode hr = doc.CreateElement("hr");
        node.PrependChild(hr);

以及破译后的结果:

<main id="main2">
        <div class="text-block">
            <h2 class="h3"></h2>
            <p>&nbsp;</p>
            <p>Big increases in sales can actually stunt business growth when a B2B company isn't prepared to meet the challenges in their online ecommerce operation such as making manual re-entries into back-office software, matching store data and inventory data and troubleshooting slowdowns and errors that create customer dissatisfaction. Manual entry and processes are just too antiquated, and even ERP and CRM software can't do the job properly unless the online store and back-office software communicate seamlessly and bidirectionally.</p>
            <p>The 
                <a href="/ecommerce-integrations/erp/oracle-integration">benefits of an ecommerce &amp; Oracle ERP integration</a> include the automation of most business processes. Data is validated outside the firewall before being accepted into the system, but this is accomplished in microseconds. Security protection is stronger while speeding the real-time dissemination of critical information and intelligence.</p>
    ---->   <p><hr>
                <strong>Internal Management Benefits of integration with Oracle E-Business Suite</strong>
            </p>
            <p>Oracle E-Business Suite, or EBS, is a group of software applications that work with Oracle's acclaimed database software. The benefits of 
                <a href="/ecommerce-integrations/erp/oracle-integration">integration with Oracle E-Business Suite</a> include an 
                <a href="/ecommerce-integrations/erp/oracle-integration">automated point-to-point integration of Oracle CRM and ERP Platforms</a>. The E-Business Suite includes a range of application modules for different business needs including various aspects of CRM and ERP for wholesalers, retail sellers, distributors and manufacturers. Customer can get what they need as Software-as-a-Service, or SaaS, applications. [1]</p>
            <p>The benefits of integrating Oracle software applications for internal management include the following:</p>
            <p>
                <strong>Catalog Management</strong>
            </p>