循环遍历 XDocument 的后代 - C#

Posted

技术标签:

【中文标题】循环遍历 XDocument 的后代 - C#【英文标题】:Looping through Descendants of an XDocument - C# 【发布时间】:2021-08-28 08:43:21 【问题描述】:

有人知道为什么下面的代码在 XDocument 中找不到任何名为“PntList3D”的后代吗?我已经使用来自不同来源的 xml 文件运行了类似的代码,并且运行良好。

XDocument xdoc = XDocument.Load(file);
foreach (XElement pntList3D in xdoc.Descendants("PntList3D"))
    
        //No XElements found?
        string[] coords = pntList3D.Value.Split(separator);
        //........
    

XML文件如下:

<?xml version="1.0" encoding="iso-8859-1" ?>
<LandXML xmlns="http://www.landxml.org/schema/LandXML-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.0 http://www.landxml.org/schema/landxml-1.0/LandXML-1.0.xsd" version="1.0" date="2018-05-21" time="10:54:45">
    <Units>
        <Metric areaUnit="squareMeter" linearUnit="meter" volumeUnit="cubicMeter" temperatureUnit="celsius" pressureUnit="HPA" angularUnit="radians" directionUnit="radians"/>
    </Units>
    <Application name="Bentley Rail Track V8i (SELECTseries 4)" manufacturer="Bentley Systems, Inc." version="08.11.09.878" manufacturerURL="www.bentley.com"/>
    <Surfaces>
        <Surface name="GUCanalFinalGround" state="proposed">
            <SourceData>
                <Breaklines>
                    <Breakline brkType="standard" name="Merge Boundary Breakline13">
                        <PntList3D>
                            373369.843784061 201940.481405058 100.010000000 373346.803045009 201978.937591557 100.266579521 373345.131682429 201986.094864832 100.285820869 373344.675829289 201994.051291911 100.305094741 373343.948058537 202002.377250983 100.325783280 373347.097559184 202015.927410062 100.351677422 373349.064121068 202028.461854698 100.377340245 373349.158000706 202037.105375695 100.397451931 373349.558848160 202047.019625644 100.420035342 373351.626002592 202059.961277642 100.444814507 373351.012091134 202068.660831945 100.465852191 373341.758274428 202103.172391947 100.582514748 373319.443256788 202115.548623449 100.621330067 373314.187088077 202116.429655954 100.632560880 373309.015283797 202117.464532283 100.644344390 373296.621004758 202116.103443765 100.663523942 373291.562007714 202117.732947895 100.675479985 373281.143675082 202119.484060498 100.698356878 373276.337524008 202120.933216769 100.710662078 373272.233432380 202123.836471532 100.724192638 373261.827555133 202125.960550682 100.747355849 373251.520869727 202128.251385445 100.770700659 373245.559103071 202128.214153277 100.781125221 373239.789665844 202128.349784236 100.791427303 373229.601950129 202130.619067512 100.814814992 373225.304350337 202132.986580328 100.828137161 373214.890815224 202136.688095193 100.839157834 373210.210442728 202138.686367756 100.852302328 373204.606106857 202142.207609670 100.866054658 373191.545497214 202148.081237003 100.918005418 373176.562316984 202152.361356766 100.954250454 373171.044645747 202154.053893695 100.966278732 373166.208397576 202155.713237010 100.978687738 373160.708995109 202155.679722689 100.989287494 373155.688534545 202156.493295914 101.000834112 373150.482235484 202157.176413548 101.012161143 373144.212549179 202156.159209438 101.021467326 373137.448440098 202156.600718164 101.031754759 373131.391817655 202156.182136957 101.041765542 373125.390422036 202155.709761447 101.051750480 373119.276552408 202155.069671573 101.061523722 373054.430807066 202143.721890221 102.441500000 373058.442814638 202140.697787490 102.328000000 373066.166069761 202134.158949750 101.933000000 373073.565553867 202127.092819841 101.360000000 373080.608667856 202119.447463058 100.589000000 373087.692942040 202111.870287954 99.839000000 373091.238928264 202108.087310204 99.465000000 373092.286790268 202109.791671688 99.504983646 373095.832728759 202106.008743033 99.130988179 373102.878820288 202098.368395826 98.353988013 373110.583835192 202091.800057704 97.934987722 373118.123075009 202084.962016843 97.421992176 373125.392085793 202077.684336252 96.759009538 373132.990746979 202070.942967185 96.274010799 373140.640127087 202064.284112116 95.814017469 373150.541354297 202061.288814036 96.578880626 373155.592889845 202050.403490658 94.696924444 373161.444949212 202040.820548117 93.249013491 373170.040905925 202035.701685379 93.293999781 373178.501762418 202030.363025321 93.264000224 373187.026261039 202025.127904379 93.265000042 373195.553374235 202019.897036065 93.265999882 373204.080773941 202014.666632815 93.264000015 373212.683146019 202009.558202046 93.299998484 373221.146334288 202004.223330229 93.258000145 373229.732436104 201999.088427042 93.279999413 373238.278154811 201993.887822981 93.277999879 373246.827935106 201988.693825711 93.275000301 373255.419702686 201983.568137077 93.292999519 373270.410218672 201988.852661245 96.796999371 373278.832148334 201983.450659530 96.717001898 373287.478398532 201978.413607052 96.756999575 373296.854035216 201974.563203038 97.191991671 373306.521867266 201971.188175126 97.782985073 373316.068961675 201967.616716606 98.305980919 373325.538253354 201963.918678862 98.785012746 373334.774518673 201959.841526182 99.132990669 373333.727060669 201958.136888591 99.093000000 373342.581640977 201953.439507703 99.231000000 373351.873048413 201949.452662854 99.604000000 373360.885734860 201945.010817605 99.823000000 373369.843784061 201940.481405058 100.010000000
                        </PntList3D>
                        <Feature code="Breakline">
                            <Property label="guid" value="210f4f9d-6f98-4246-8967-4b11eb616339"/>
                            <Property label="style" value="Default"/>
                            <Property label="triangulate" value="true"/>
                            <Property label="pointDensity" value="0.000000000"/>
                        </Feature>
                    </Breakline>
                    <Breakline brkType="standard" name="DB75">
                        <PntList3D>
                            373095.450629438 202114.938959557 101.014871165 373092.286790268 202109.791671688 99.504983646
                        </PntList3D>
                        <Feature code="Breakline">
                            <Property label="guid" value="9d937eeb-bcf1-4d51-97c1-080736d60adb"/>
                            <Property label="style" value="Default"/>
                            <Property label="triangulate" value="true"/>
                            <Property label="pointDensity" value="0.000000000"/>
                        </Feature>
                    </Breakline>
                    <Breakline brkType="standard" name="DB74">
                        <PntList3D>
                            373054.430807066 202143.721890221 102.441500000 373095.450629438 202114.938959557 101.014871165
                        </PntList3D>
                        <Feature code="Breakline">
                            <Property label="guid" value="71b6272a-99f3-483c-b997-4b3b82de6984"/>
                            <Property label="style" value="Default"/>
                            <Property label="triangulate" value="true"/>
                            <Property label="pointDensity" value="0.000000000"/>
                        </Feature>
                    </Breakline>
                </Breaklines>
            </SourceData>
        </Surface>
    </Surfaces>
</LandXML>

我无法提交这个问题,因为显然我的代码太多而细节不够。我很确定我提供的详细信息已经足够了,所以下面的文字只是为了通过 *** 机器人解决这个问题。

【问题讨论】:

你有一个默认的命名空间。尝试以下操作:xdoc.Descendants().Where(x => x.Name.LocalName =="PntList3D") 【参考方案1】:

请尝试以下方法。

整个 XML 绑定到默认命名空间。

首先,您只需获取一次默认命名空间。 其次,在引用 XML 元素名称时随时使用它。

c#

void Main()

    const string FILENAME = @"e:\Temp\LandXML.xml";

    XDocument xdoc = XDocument.Load(FILENAME);
    XNamespace ns = xdoc.Root.GetDefaultNamespace();

    foreach (XElement pntList3D in xdoc.Descendants(ns + "PntList3D"))
    
        Console.WriteLine(pntList3D.Value + Environment.NewLine);
    

【讨论】:

谢谢,我相信给出的两个答案都是有效的,但我最终使用了这个。我必须做的唯一改变是我必须添加修剪 pntList3D.Value.Trim() 以删除数据上的一堆制表符 \t 和换行符 \n。老实说,不知道为什么会选择他们。【参考方案2】:

您需要在调用中为后代指定命名空间:

foreach (var pntList3D in xdoc.Descendants(XName.Get("PntList3D","http://www.landxml.org/schema/LandXML-1.0")))

        //No XElements found?
        string[] coords = pntList3D.Value.Split(separator);
        //........

【讨论】:

以上是关于循环遍历 XDocument 的后代 - C#的主要内容,如果未能解决你的问题,请参考以下文章

使用属性过滤器读取 XDocument 的后代元素

如何使用 Foreach 遍历集合以构建 XDocument?

Web开发——JavaScript库(jQuery遍历——后代)

jQuery 遍历 – 后代

jQuery遍历-后代

遍历祖先同胞后代