简体   繁体   中英

C# - LINQ to XML - Find Descendants where the attribute starts with something

I have attributes that start the same but have a different end. For example "Process.205", "Process.101".

Originally I'm using it like this but this is only for an exact name

public string XmlFindSurveyName(XElement pageContents, XNamespace nameSpace)
        {
            var docName = "";
            var datasets = pageContents.Descendants(nameSpace + "Shape").Where(x => (string)x.Parent.Parent.Attribute("Name") == "CFF Container.25");
            foreach (var data in datasets)
            {
                var text = data.Element(nameSpace + "Text");
                if (text != null)
                {
                    docName = data.Element(nameSpace + "Text").Value;
                }

            }
            return docName;
        }

My attempt at trying to get any that start with "CFF Container" looks like the below but errors

public string XmlFindSurveyName(XElement pageContents, XNamespace nameSpace)
        {
            var docName = "";
            var datasets = pageContents.Descendants(nameSpace + "Shape").Where(x => x.Parent.Parent.Attribute("Name").Value.StartsWith("CFF Container"));
            foreach (var data in datasets)
            {
                var text = data.Element(nameSpace + "Text");
                if (text != null)
                {
                    docName = data.Element(nameSpace + "Text").Value;
                }

            }
            return docName;
        }

在此处输入图像描述 The below is the XML (I've cut some of it off cus its quite long)

<?xml version='1.0' encoding='utf-8'?>
<PageContents xmlns='http://schemas.microsoft.com/office/visio/2012/main' xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' xml:space='preserve'>
    <Shapes>
        <Shape ID='134' NameU='CFF Container.25' Name='CFF Container.25' Type='Group' Master='25' UniqueID='{5CCC8F6C-57F9-4C96-974F-4067BF3CC683}'>
            <Cell N='PinX' V='7.381890125792073' />
            <Cell N='PinY' V='5.905511468485626' />
            <Cell N='Width' V='14.76377969045288' />
            <Cell N='Height' V='11.81102333228352' />
            <Cell N='LocPinX' V='7.38188984522644' F='Inh' />
            <Cell N='LocPinY' V='5.905511666141847' F='Inh' />
            <Cell N='Relationships' V='0' F='SUM(DEPENDSON(1,Sheet.138!SheetRef(),Sheet.137!SheetRef()))' />
            <Cell N='LineWeight' V='0.01' U='PT' />
            <Cell N='Rounding' V='0' U='MM' />
            <Cell N='FillForegnd' V='1' />
            <Cell N='FillBkgnd' V='#b7c9e3' F='HSL(144,106,193)' />
            <Cell N='ShdwForegnd' V='#ffffff' F='HSL(0,0,240)' />
            <Cell N='ShdwPattern' V='0' F='GUARD(0)' />
            <Cell N='ShapeShdwOffsetX' V='0' U='MM' />
            <Cell N='ShapeShdwOffsetY' V='0' U='MM' />
            <Cell N='ShapeShdwObliqueAngle' V='0' U='DEG' />
            <Section N='User'>
                <Row N='msvSDContainerLocked'>
                    <Cell N='Value' V='1' U='BOOL' />
                </Row>
                <Row N='CFFVertical'>
                    <Cell N='Value' V='1' />
                </Row>
                <Row N='numLanes'>
                    <Cell N='Value' V='1' />
                </Row>
            </Section>
            <Section N='Connection'>
                <Row T='Connection' IX='0'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='0.7381889582677309' F='Inh' />
                </Row>
                <Row T='Connection' IX='1'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='1.476377916535462' F='Inh' />
                </Row>
                <Row T='Connection' IX='2'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='2.214566874803193' F='Inh' />
                </Row>
                <Row T='Connection' IX='3'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='2.952755833070924' F='Inh' />
                </Row>
                <Row T='Connection' IX='4'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='3.690944791338655' F='Inh' />
                </Row>
                <Row T='Connection' IX='5'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='4.429133749606385' F='Inh' />
                </Row>
                <Row T='Connection' IX='6'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='5.167322707874116' F='Inh' />
                </Row>
                <Row T='Connection' IX='7'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='5.905511666141847' F='Inh' />
                </Row>
                <Row T='Connection' IX='8'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='6.643700624409578' F='Inh' />
                </Row>
                <Row T='Connection' IX='9'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='7.381889582677309' F='Inh' />
                </Row>
                <Row T='Connection' IX='10'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='8.120078540945039' F='Inh' />
                </Row>
                <Row T='Connection' IX='11'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='8.85826749921277' F='Inh' />
                </Row>
                <Row T='Connection' IX='12'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='9.596456457480501' F='Inh' />
                </Row>
                <Row T='Connection' IX='13'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='10.33464541574823' F='Inh' />
                </Row>
                <Row T='Connection' IX='14'>
                    <Cell N='X' V='0' F='Inh' />
                    <Cell N='Y' V='11.07283437401596' F='Inh' />
                </Row>
                <Row T='Connection' IX='15'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='0.7381889582677309' F='Inh' />
                </Row>
                <Row T='Connection' IX='16'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='1.476377916535462' F='Inh' />
                </Row>
                <Row T='Connection' IX='17'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='2.214566874803193' F='Inh' />
                </Row>
                <Row T='Connection' IX='18'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='2.952755833070924' F='Inh' />
                </Row>
                <Row T='Connection' IX='19'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='3.690944791338655' F='Inh' />
                </Row>
                <Row T='Connection' IX='20'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='4.429133749606385' F='Inh' />
                </Row>
                <Row T='Connection' IX='21'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='5.167322707874116' F='Inh' />
                </Row>
                <Row T='Connection' IX='22'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='5.905511666141847' F='Inh' />
                </Row>
                <Row T='Connection' IX='23'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='6.643700624409578' F='Inh' />
                </Row>
                <Row T='Connection' IX='24'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='7.381889582677309' F='Inh' />
                </Row>
                <Row T='Connection' IX='25'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='8.120078540945039' F='Inh' />
                </Row>
                <Row T='Connection' IX='26'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='8.85826749921277' F='Inh' />
                </Row>
                <Row T='Connection' IX='27'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='9.596456457480501' F='Inh' />
                </Row>
                <Row T='Connection' IX='28'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='10.33464541574823' F='Inh' />
                </Row>
                <Row T='Connection' IX='29'>
                    <Cell N='X' V='14.76377969045288' F='Inh' />
                    <Cell N='Y' V='11.07283437401596' F='Inh' />
                </Row>
            </Section>
            <Shapes>
                <Shape ID='135' Type='Shape' MasterShape='6'>
                    <Cell N='PinX' V='7.38188984522644' F='Inh' />
                    <Cell N='PinY' V='5.905511666141847' F='Inh' />
                    <Cell N='Width' V='14.76377969045288' F='Inh' />
                    <Cell N='Height' V='11.81102333228369' F='Inh' />
                    <Cell N='LocPinX' V='7.38188984522644' F='Inh' />
                    <Cell N='LocPinY' V='5.905511666141847' F='Inh' />
                    <Cell N='LineWeight' V='0.01' U='PT' F='Inh' />
                    <Section N='Geometry' IX='0'>
                        <Row T='LineTo' IX='2'>
                            <Cell N='X' V='14.76377969045288' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='3'>
                            <Cell N='X' V='14.76377969045288' F='Inh' />
                            <Cell N='Y' V='11.81102333228369' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='4'>
                            <Cell N='Y' V='11.81102333228369' F='Inh' />
                        </Row>
                    </Section>
                </Shape>
                <Shape ID='136' Type='Shape' MasterShape='7' UniqueID='{DF532DE2-3981-4C9F-A9AF-C7343AC06514}'>
                    <Cell N='PinX' V='7.38188984522644' F='Inh' />
                    <Cell N='PinY' V='11.61417293858291' U='MM' F='Inh' />
                    <Cell N='Width' V='14.76377969045288' F='Inh' />
                    <Cell N='LocPinX' V='7.38188984522644' F='Inh' />
                    <Cell N='TxtPinX' V='7.38188984522644' F='Inh' />
                    <Cell N='TxtWidth' V='14.76377969045288' F='Inh' />
                    <Cell N='TxtLocPinX' V='7.38188984522644' F='Inh' />
                    <Cell N='LineWeight' V='0.003333333333333333' U='PT' />
                    <Cell N='LineColor' V='#000000' F='HSL(0,0,0)' />
                    <Cell N='Rounding' V='0' U='MM' />
                    <Cell N='FillForegnd' V='1' />
                    <Cell N='FillBkgnd' V='#b7c9e3' F='HSL(144,106,193)' />
                    <Cell N='FillPattern' V='1' />
                    <Cell N='ShdwForegnd' V='#ffffff' F='HSL(0,0,240)' />
                    <Cell N='ShdwPattern' V='0' F='GUARD(0)' />
                    <Cell N='ShapeShdwOffsetX' V='0' U='MM' />
                    <Cell N='ShapeShdwOffsetY' V='0' U='MM' />
                    <Cell N='ShapeShdwObliqueAngle' V='0' U='DEG' />
                    <Cell N='LeftMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='RightMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='TopMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='BottomMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='DefaultTabStop' V='0.5905511811023622' U='MM' />
                    <Section N='Paragraph'>
                        <Row IX='0'>
                            <Cell N='IndFirst' V='0' U='MM' />
                            <Cell N='IndLeft' V='0' U='MM' />
                            <Cell N='IndRight' V='0' U='MM' />
                            <Cell N='SpBefore' V='0' U='PT' />
                            <Cell N='SpAfter' V='0' U='PT' />
                            <Cell N='TextPosAfterBullet' V='0' U='MM' />
                        </Row>
                    </Section>
                    <Section N='Character'>
                        <Row IX='0'>
                            <Cell N='Size' V='0.1944444444444444' U='PT' />
                            <Cell N='LangID' V='en-GB' />
                        </Row>
                    </Section>
                    <Section N='Geometry' IX='0'>
                        <Row T='LineTo' IX='2'>
                            <Cell N='X' V='14.76377969045288' U='MM' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='3'>
                            <Cell N='X' V='14.76377969045288' U='MM' F='Inh' />
                        </Row>
                    </Section>
                    <Section N='Geometry' IX='1'>
                        <Row T='EllipticalArcTo' IX='2'>
                            <Cell N='X' V='14.76377969045288' F='Inh' />
                            <Cell N='A' V='7.38188984522644' U='DL' F='Inh' />
                            <Cell N='C' V='-5.893892986996718E-7' U='DA' F='Inh' />
                            <Cell N='D' V='18.75000020612266' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='3'>
                            <Cell N='X' V='14.76377969045288' F='Inh' />
                        </Row>
                    </Section>
                    <Section N='Geometry' IX='2'>
                        <Row T='LineTo' IX='2'>
                            <Cell N='X' V='14.76377969045288' U='MM' F='Inh' />
                        </Row>
                    </Section>
                    <Text>
                        <cp IX='0' />
                        <pp IX='0' />
                        Test Doc
                    </Text>
                </Shape>
            </Shapes>
        </Shape>
        <Shape ID='137' NameU='Swimlane List' Name='Swimlane List' Type='Shape' Master='26' UniqueID='{2A8BE7B5-3ECE-42F5-BF85-24B6AAF9A6A7}'>
            <Cell N='PinX' V='0.3937010679672476' />
            <Cell N='PinY' V='11.41732234722591' />
            <Cell N='Width' V='14.37007890305125' />
            <Cell N='Height' V='11.4173225448821' />
            <Cell N='LocPinY' V='11.41732254488212' F='Inh' />
            <Cell N='Relationships' V='0' F='SUM(DEPENDSON(1,Sheet.366!SheetRef(),Sheet.364!SheetRef(),Sheet.205!SheetRef()),DEPENDSON(11,Sheet.138!SheetRef()),DEPENDSON(2,Sheet.139!SheetRef()),DEPENDSON(4,Sheet.134!SheetRef()))' />
            <Section N='User'>
                <Row N='msvSDContainerStyle'>
                    <Cell N='Value' V='1' F='IFERROR(CONTAINERSHEETREF(1)!User.VISCFFSTYLE,1)' />
                </Row>
                <Row N='msvSDListDirection'>
                    <Cell N='Value' V='0' />
                </Row>
                <Row N='msvSDListItemMaster'>
                    <Cell N='Value' V='254' F='USE("Swimlane (vertical)")' />
                </Row>
                <Row N='visHeadingHeight'>
                    <Cell N='Value' V='0.4921259842519685' U='MM' />
                </Row>
            </Section>
            <Section N='Scratch'>
                <Row IX='0'>
                    <Cell N='A' V='0' F='IFERROR(SETF(GetRef(CONTAINERSHEETREF(1)!User.NUMLANES),LISTMEMBERCOUNT()),0)' />
                </Row>
            </Section>
            <Section N='Geometry' IX='0'>
                <Row T='LineTo' IX='2'>
                    <Cell N='X' V='14.37007890305127' F='Inh' />
                </Row>
                <Row T='LineTo' IX='3'>
                    <Cell N='X' V='14.37007890305127' F='Inh' />
                    <Cell N='Y' V='11.41732254488212' F='Inh' />
                </Row>
                <Row T='LineTo' IX='4'>
                    <Cell N='Y' V='11.41732254488212' F='Inh' />
                </Row>
            </Section>
        </Shape>
        <Shape ID='139' NameU='Swimlane (vertical).28' Name='Swimlane (vertical).28' Type='Group' Master='28' UniqueID='{004E2343-CE1D-4FE5-A5FE-B0D1F0F2A68F}'>
            <Cell N='PinX' V='7.578740519492888' />
            <Cell N='PinY' V='5.708661074784847' />
            <Cell N='Width' V='14.37007890305125' />
            <Cell N='Height' V='11.4173225448821' />
            <Cell N='LocPinX' V='7.185039451525626' F='Inh' />
            <Cell N='LocPinY' V='5.70866127244106' F='Inh' />
            <Cell N='Relationships' V='0' F='SUM(DEPENDSON(5,Sheet.137!SheetRef()),DEPENDSON(1,Sheet.366!SheetRef(),Sheet.364!SheetRef(),Sheet.205!SheetRef()))' />
            <Cell N='LineWeight' V='0.003333333333333333' U='PT' />
            <Cell N='LineColor' V='#000000' F='HSL(0,0,0)' />
            <Cell N='Rounding' V='0' U='MM' />
            <Cell N='FillForegnd' V='#ffffff' F='THEMEGUARD(RGB(255,255,255))' />
            <Cell N='FillBkgnd' V='#ffffff' F='THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME("FillColor"),THEME("FillColor2"))))' />
            <Cell N='ShdwForegnd' V='#ffffff' F='HSL(0,0,240)' />
            <Cell N='ShdwPattern' V='0' />
            <Cell N='ShapeShdwOffsetX' V='0' U='MM' />
            <Cell N='ShapeShdwOffsetY' V='0' U='MM' />
            <Cell N='ShapeShdwObliqueAngle' V='0' U='DEG' />
            <Section N='User'>
                <Row N='msvSDContainerMargin'>
                    <Cell N='Value' V='0.2952755905511811' U='MM' F='Sheet.134!User.visMargins' />
                </Row>
                <Row N='visDirection'>
                    <Cell N='Value' V='1' F='IF(Sheet.137!User.msvSDListDirection=0,1,IF(Sheet.137!User.msvSDListDirection=1,0,IF(Sheet.138!User.msvSDListDirection=0,1,0)))' />
                </Row>
                <Row N='RTL'>
                    <Cell N='Value' V='0' F='IFERROR(CONTAINERSHEETREF(LISTSHEETREF(),1,"CFF Container")!User.RTL,0)' />
                </Row>
                <Row N='visRotateLabel'>
                    <Cell N='Value' V='0' F='IFERROR(LISTSHEETREF()!User.VISROTATELABEL,0)' />
                </Row>
                <Row N='visHeadingText'>
                    <Cell N='Value' V='Test Page' U='STR' F='Inh' />
                </Row>
                <Row N='visCFFStyle'>
                    <Cell N='Value' V='1' F='Sheet.134!User.visCFFStyle' />
                </Row>
                <Row N='SwimlaneListGUID'>
                    <Cell N='Value' V='{493ACA54-BE30-458F-A9CD-620051232637}' U='GUID' />
                </Row>
                <Row N='visCFFSettings'>
                    <Cell N='Value' V='/stg1=1, /stg2=1, /stg3=1, /stg4=1, /stg5=0.2953, /stg6=0' U='STR' F='"/stg1="&amp;Sheet.134!User.visCFFStyle&amp;", /stg2="&amp;Sheet.134!User.visShowPhase&amp;", /stg3="&amp;Sheet.134!User.visShowTitle&amp;", /stg4="&amp;Sheet.134!User.numLanes&amp;", /stg5="&amp;FORMATEX(Sheet.134!User.visMargins,"0.0000","","in")&amp;", /stg6="&amp;Sheet.137!User.visRotateLabel' />
                </Row>
                <Row N='LineWeight'>
                    <Cell N='Value' V='0.01' U='PT' F='IFERROR(CONTAINERSHEETREF(LISTSHEETREF(),1,"CFF Container")!LineWeight,THEMEVAL("LineWeight"))' />
                </Row>
            </Section>
            <Shapes>
                <Shape ID='140' Type='Shape' MasterShape='6' UniqueID='{191BB5AA-360E-41C0-955F-1DB7E36E86E4}'>
                    <Cell N='PinX' V='7.185039451525626' F='Inh' />
                    <Cell N='PinY' V='5.70866127244106' F='Inh' />
                    <Cell N='Width' V='14.37007890305125' F='Inh' />
                    <Cell N='Height' V='11.41732254488212' F='Inh' />
                    <Cell N='LocPinX' V='7.185039451525626' F='Inh' />
                    <Cell N='LocPinY' V='5.70866127244106' F='Inh' />
                    <Cell N='LineWeight' V='0.003333333333333333' U='PT' />
                    <Cell N='LineColor' V='#000000' F='HSL(0,0,0)' />
                    <Cell N='Rounding' V='0' U='MM' />
                    <Cell N='FillForegnd' V='#ffffff' F='THEMEGUARD(RGB(255,255,255))' />
                    <Cell N='FillBkgnd' V='#ffffff' F='THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME("FillColor"),THEME("FillColor2"))))' />
                    <Section N='Geometry' IX='0'>
                        <Row T='LineTo' IX='2'>
                            <Cell N='X' V='14.37007890305125' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='3'>
                            <Cell N='X' V='14.37007890305125' F='Inh' />
                            <Cell N='Y' V='11.41732254488212' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='4'>
                            <Cell N='Y' V='11.41732254488212' F='Inh' />
                        </Row>
                    </Section>
                </Shape>
                <Shape ID='141' Type='Shape' MasterShape='7' UniqueID='{C4DE15A7-4B27-4247-B542-DBC54774A9B7}'>
                    <Cell N='PinX' V='7.185039451525626' F='Inh' />
                    <Cell N='PinY' V='11.17125955275614' U='MM' F='Inh' />
                    <Cell N='Width' V='14.37007890305125' F='Inh' />
                    <Cell N='Height' V='0.4921259842519685' U='MM' F='Sheet.137!User.visHeadingHeight' />
                    <Cell N='LocPinX' V='7.185039451525626' F='Inh' />
                    <Cell N='LocPinY' V='0.2460629921259843' U='MM' F='Inh' />
                    <Cell N='TxtPinX' V='7.185039451525626' F='Inh' />
                    <Cell N='TxtPinY' V='0.2460629921259843' U='MM' F='Inh' />
                    <Cell N='TxtWidth' V='14.37007890305125' F='Inh' />
                    <Cell N='TxtHeight' V='0.4921259842519685' U='MM' F='Inh' />
                    <Cell N='TxtLocPinX' V='7.185039451525626' F='Inh' />
                    <Cell N='TxtLocPinY' V='0.2460629921259843' U='MM' F='Inh' />
                    <Cell N='LineWeight' V='0.003333333333333333' U='PT' />
                    <Cell N='LineColor' V='#000000' F='HSL(0,0,0)' />
                    <Cell N='Rounding' V='0' U='MM' />
                    <Cell N='FillForegnd' V='#ffffff' F='THEMEGUARD(RGB(255,255,255))' />
                    <Cell N='FillBkgnd' V='#ffffff' F='THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME("FillColor"),THEME("FillColor2"))))' />
                    <Cell N='FillPattern' V='1' />
                    <Cell N='ShdwForegnd' V='#ffffff' F='HSL(0,0,240)' />
                    <Cell N='ShdwPattern' V='0' />
                    <Cell N='ShapeShdwOffsetX' V='0' U='MM' />
                    <Cell N='ShapeShdwOffsetY' V='0' U='MM' />
                    <Cell N='ShapeShdwObliqueAngle' V='0' U='DEG' />
                    <Cell N='LeftMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='RightMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='TopMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='BottomMargin' V='0.02777777777777778' U='PT' />
                    <Cell N='DefaultTabStop' V='0.5905511811023622' U='MM' />
                    <Section N='User'>
                        <Row N='visHeadingHeight'>
                            <Cell N='Value' V='0.4921259842519685' U='MM' F='Inh' />
                        </Row>
                        <Row N='HeadingPos'>
                            <Cell N='Value' V='2' F='IF(OR(Sheet.137!User.msvSDListDirection=0,Sheet.137!User.msvSDListDirection=1),2,IF(Sheet.138!User.msvSDListDirection=0,3,1))' />
                        </Row>
                    </Section>
                    <Section N='Paragraph'>
                        <Row IX='0'>
                            <Cell N='IndFirst' V='0' U='MM' />
                            <Cell N='IndLeft' V='0' U='MM' />
                            <Cell N='IndRight' V='0' U='MM' />
                            <Cell N='SpBefore' V='0' U='PT' />
                            <Cell N='SpAfter' V='0' U='PT' />
                            <Cell N='TextPosAfterBullet' V='0' U='MM' />
                        </Row>
                    </Section>
                    <Section N='Character'>
                        <Row IX='0'>
                            <Cell N='LangID' V='en-GB' />
                        </Row>
                    </Section>
                    <Section N='Geometry' IX='0'>
                        <Row T='LineTo' IX='2'>
                            <Cell N='X' V='14.37007890305125' U='MM' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='3'>
                            <Cell N='X' V='14.37007890305125' U='MM' F='Inh' />
                            <Cell N='Y' V='0.4921259842519685' U='MM' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='4'>
                            <Cell N='Y' V='0.4921259842519685' U='MM' F='Inh' />
                        </Row>
                    </Section>
                    <Section N='Geometry' IX='1'>
                        <Row T='MoveTo' IX='1'>
                            <Cell N='Y' V='-0.09842519685039371' U='MM' F='Inh' />
                        </Row>
                        <Row T='EllipticalArcTo' IX='2'>
                            <Cell N='X' V='14.37007890305125' F='Inh' />
                            <Cell N='Y' V='-0.09842519685039371' U='MM' F='Inh' />
                            <Cell N='A' V='7.185039451525626' U='DL' F='Inh' />
                            <Cell N='C' V='-7.583257091159368E-7' U='DA' F='Inh' />
                            <Cell N='D' V='14.60000016491744' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='3'>
                            <Cell N='X' V='14.37007890305125' F='Inh' />
                            <Cell N='Y' V='0.4921259842519685' U='MM' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='4'>
                            <Cell N='Y' V='0.4921259842519685' U='MM' F='Inh' />
                        </Row>
                        <Row T='LineTo' IX='5'>
                            <Cell N='Y' V='-0.09842519685039371' U='MM' F='Inh' />
                        </Row>
                    </Section>
                    <Section N='Geometry' IX='2'>
                        <Row T='LineTo' IX='2'>
                            <Cell N='X' V='14.37007890305125' U='MM' F='Inh' />
                        </Row>
                    </Section>
                    <Text>
                        <cp IX='0' />
                        <pp IX='0' />
                        Test Page
                    </Text>
                </Shape>
            </Shapes>
        </Shape>
    </Shapes>
</PageContents>

I think I can chain my way down and have this work without the ".Parent" but it would be a lot easier to work with if there is a way to keep that as it lets me do it all in one go. Though I'm starting to think maybe by doing it this way I'm making it harder than it needs to be

Your issue is that x.Parent.Parent.Attribute("Name") doesn't exist for the first Shape in your Xml. The parent of the parent is PageContents root of the document and that doesn't have a Name attribute.

You could solve easily like this:

var datasets =
    pageContents
        .Descendants(nameSpace + "Shape")
        .Where(x => x.Parent.Parent.Name == nameSpace + "Shape")
        .Where(x => x.Parent.Parent.Attribute("Name").Value.StartsWith("CFF Container"));

You can also write your whole code like this:

public string XmlFindSurveyName(XElement pageContents, XNamespace nameSpace) =>
    (
        from shape in pageContents.Descendants(nameSpace + "Shape")
        where shape.Parent.Parent.Name == nameSpace + "Shape"
        where shape.Parent.Parent.Attribute("Name").Value.StartsWith("CFF Container")
        where shape.Element(nameSpace + "Text") != null
        select shape.Element(nameSpace + "Text").Value
    ).FirstOrDefault() ?? "";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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