string xml = @"<?xml version='1.0' encoding='utf-8'?> <ComputerBuildReportRequest xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.ms.it.oem/digitaldistribution/2010/10'> <Bindings> <Binding> <ProductKeyID>1000000002</ProductKeyID> <HardwareHash>xxx</HardwareHash> <HXSpecifics> <UnitPartNumber>123456-789</UnitPartNumber> </HXSpecifics> </Binding> <Binding> <ProductKeyID>1000000003</ProductKeyID> <HardwareHash>yyy</HardwareHash> <HXSpecifics> <UnitPartNumber>123456-787</UnitPartNumber> </HXSpecifics> </Binding> </Bindings> </ComputerBuildReportRequest>"; XDocument doc = XDocument.Parse(xml); var elements = doc.Descendants("Binding"); // empty以前就碰過,一直懶得研究 XDocument 到底為什麼有時候會秀逗,直接改用 XmlDocument。 這次碰到非用不可的情況,就藉機深究下。
簡單說,有 namespace 的情況下查都要補 namespace 給它,非常智障。
string xml = @"<?xml version='1.0' encoding='utf-8'?> <ComputerBuildReportRequest xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.ms.it.oem/digitaldistribution/2010/10'> <Bindings> <Binding> <ProductKeyID>1000000002</ProductKeyID> <HardwareHash>xxx</HardwareHash> <HXSpecifics> <UnitPartNumber>123456-789</UnitPartNumber> </HXSpecifics> </Binding> <Binding> <ProductKeyID>1000000003</ProductKeyID> <HardwareHash>yyy</HardwareHash> <HXSpecifics> <UnitPartNumber>123456-787</UnitPartNumber> </HXSpecifics> </Binding> </Bindings> </ComputerBuildReportRequest>"; XDocument doc = XDocument.Parse(xml); XNamespace df = doc.Root.Name.Namespace; var elements = doc.Descendants(df + "Binding");而且這用法還不接受$串字的語法糖,是真的棒。
沒有留言:
張貼留言