菜鸟求助取XML节点的值。
我想取出XML文件里面所有的orders节点以及下面所有子节点的值。
XML文件如下
- XML code
<marketlive xmlns="http://marketlive.com/integration/xmlbean"> <results> <info> <timeStamp>Fri Mar 02 10:03:03 PST 2012</timeStamp> <site>jessicasimpson</site> <environment>production</environment> <vmType/> <host/> <vmId>jessicasimpson-prod-iws1</vmId> <systemLocale>en</systemLocale> <driver>oracle.jdbc.driver.OracleDataSource</driver> <dbUrl>jdbc:oracle:thin:@10.100.3.60:1523:SAPRD7G1</dbUrl> </info> <result><output><orders><order pk="107101"> <code>ML_107101</code> <dateCreated>2012-03-02T09:37:13.889-08:00</dateCreated> <dateModified>2012-03-02T09:37:13.924-08:00</dateModified> <siteCode>jessicasimpson</siteCode> <locale>en_US</locale> <customerCode>ML_180988</customerCode> <status>Ordered</status> <subTotal>88.20</subTotal> <taxTotal>0.00</taxTotal> <shippingTotal>22.00</shippingTotal> <shippingCostTotal>7.00</shippingCostTotal> <shippingMethodTotal>15.00</shippingMethodTotal> <shippingWeightTotal>0.00</shippingWeightTotal> <shippingLocationTotal>0.00</shippingLocationTotal> <giftWrapTotal>0.00</giftWrapTotal> <additionalAddressTotal>0.00</additionalAddressTotal> <additionalChargesTotal>0.00</additionalChargesTotal> <weightSurchargeTotal>0.00</weightSurchargeTotal> <total>110.20</total> <merchandiseTotal>98.00</merchandiseTotal> <deferred>false</deferred> <multipleAddresses>false</multipleAddresses> <sourceCodesAsString>WELCOME10*|*</sourceCodesAsString> <deleted>false</deleted> <dateOrdered>2012-03-02T09:37:13.888-08:00</dateOrdered> <billToInfo pk="107502"> <code>ML_107502</code> <dateCreated>2012-03-02T09:37:13.888-08:00</dateCreated> <dateModified>2012-03-02T09:37:13.888-08:00</dateModified> <attributePages> <attributePage> <name>person</name> <attributes> <firstName>Brandi</firstName> <lastName>Pharr</lastName> </attributes> </attributePage> <attributePage> <name>address</name> <attributes> <street1>293 Marion Place</street1> <postOfficeBox>false</postOfficeBox> <city>Merritt Island</city> <state>FL</state> <country>US</country> <postalCode>32953</postalCode> </attributes> </attributePage> </attributePages> <phone1>3214463699</phone1> <email>bpharr21@yahoo.com</email> </billToInfo> <shipments> <orderShipment pk="107101"> <code>ML_107101</code> <dateCreated>2012-03-02T09:37:13.842-08:00</dateCreated> <shipToInfo pk="107501"> <code>ML_107501</code> <dateCreated>2012-03-02T09:37:13.796-08:00</dateCreated> <dateModified>2012-03-02T09:37:13.796-08:00</dateModified> <attributePages> <attributePage> <name>person</name> <attributes> <firstName>Brandi</firstName> <lastName>Pharr</lastName> </attributes> </attributePage> <attributePage> <name>address</name> <attributes> <street1>293 Marion Place</street1> <postOfficeBox>false</postOfficeBox> <city>Merritt Island</city> <state>FL</state> <country>US</country> <postalCode>32953</postalCode> </attributes> </attributePage> </attributePages> <phone1>3214463699</phone1> <email>bpharr21@yahoo.com</email> </shipToInfo> <status>Ordered</status> <shippingMethodCode>UPS1DY</shippingMethodCode> <shippingMethodName>UPS Next Day</shippingMethodName> <shippingMethodDescription>All orders placed by 10AM PST will arrive Next Business Day.</shippingMethodDescription> <subTotal>98.00</subTotal> <total>120.00</total> <shippingTotal>22.00</shippingTotal> <shippingCostTotal>7.00</shippingCostTotal> <shippingMethodTotal>15.00</shippingMethodTotal> <shippingWeightTotal>0.00</shippingWeightTotal> <taxTotal>0.00</taxTotal> <giftWrapTotal>0.00</giftWrapTotal> <additionalChargesTotal>0.00</additionalChargesTotal> <weightSurchargeTotal>0.00</weightSurchargeTotal> <merchandiseTotal>98.00</merchandiseTotal> <deleted>false</deleted> <items> <orderItem pk="107201"> <code>ML_107201</code> <dateCreated>2012-03-02T09:37:13.842-08:00</dateCreated> <categoryCode>C2-Sub Category 1</categoryCode> <productCode>CATHI</productCode> <productName>CATHI</productName> <status>Ordered</status> <regularPrice>98.00</regularPrice> <sellPrice>98.00</sellPrice> <subTotal>98.00</subTotal> <qty>1</qty> <ordinal>0</ordinal> <sku>CATHIJV2811800</sku> <skuPk>107437</skuPk> <overWeight>false</overWeight> <giftCertificate>false</giftCertificate> <options>6,NATURAL COMBO</options> <optionNames>6,Natural Combo</optionNames> <freeGift>false</freeGift> <kitPart>false</kitPart> </orderItem> </items> </orderShipment> </shipments> <discounts> <discount> <message>10% Off Discount has been applied</message> <amount>9.80</amount> <pricingName>10% OFF</pricingName> <sourceCodesAsString>WELCOME10*|*</sourceCodesAsString> </discount> </discounts></order></orders></output><summary> <total>3</total> <failed>0</failed></summary></result></results></marketlive>
[解决办法]
- C# code
static void Main(string[] args) { XmlDocument xml = new XmlDocument(); xml.Load(@"E:\a.xml"); XmlNamespaceManager xns = new XmlNamespaceManager(xml.NameTable); xns.AddNamespace("a","http://marketlive.com/integration/xmlbean"); XmlNode node = xml.SelectSingleNode("//a:orders", xns); ResponseText(node.ChildNodes); Console.ReadLine(); } protected static void ResponseText(XmlNodeList node) { foreach (XmlNode n in node) { if (n.HasChildNodes) ResponseText(n.ChildNodes); else Console.WriteLine(n.InnerText); } }/*ML_1071012012-03-02T09:37:13.889-08:002012-03-02T09:37:13.924-08:00jessicasimpsonen_USML_180988Ordered88.200.0022.007.0015.000.000.000.000.000.000.00110.2098.00falsefalseWELCOME10*|*false2012-03-02T09:37:13.888-08:00ML_1075022012-03-02T09:37:13.888-08:002012-03-02T09:37:13.888-08:00personBrandiPharraddress293 Marion PlacefalseMerritt IslandFLUS329533214463699bpharr21@yahoo.comML_1071012012-03-02T09:37:13.842-08:00ML_1075012012-03-02T09:37:13.796-08:002012-03-02T09:37:13.796-08:00personBrandiPharraddress293 Marion PlacefalseMerritt IslandFLUS329533214463699bpharr21@yahoo.comOrderedUPS1DYUPS Next DayAll orders placed by 10AM PST will arrive Next Business Day.98.00120.0022.007.0015.000.000.000.000.000.0098.00falseML_1072012012-03-02T09:37:13.842-08:00C2-Sub Category 1CATHICATHIOrdered98.0098.0098.0010CATHIJV2811800107437falsefalse6,NATURAL COMBO6,Natural Combofalsefalse10% Off Discount has been applied9.8010% OFFWELCOME10*|**/