Prev: how to activate the trial version of Windows 2008 server R2
Next: Xml // Reading Element Value // Namespace issue // XPathExpressionXmlNamespaceManager
From: sloan on 22 Feb 2010 11:27 Ok, my xml inability shows itself once again. Thanks for any help. Below I have some xml and some code for trying to get the "Publish_Date" element value. I've tried 4 different ways to get it. (If you uncomment the lines in the code seen here, you can see the ways I've tried to get the value) theOneImGonnaTry = myNameSpaceNameLocalName1 + ":"; theOneImGonnaTry = myNameSpaceNameLocalName2 + ":"; theOneImGonnaTry = myNameSpaceNameLocalName3 + ":"; theOneImGonnaTry = string.Empty; (The code seen above makes more sense when you look at the code below). Below is the xml sample (and its source url in the code) and the C# code I'm trying to use. ---------START XML <?xml version="1.0" standalone="yes"?> <sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd"> <publshInformation> <Publish_Date>02/10/2010</Publish_Date> <Record_Count>4301</Record_Count> </publshInformation> </sdnList> ---------END XML C# code below string sdnUrl = "http://www.ustreas.gov/offices/enforcement/ofac/sdn/sdn.xml?fakeParameter=" + DateTime.Now.ToLongTimeString(); XPathDocument doc = new XPathDocument(sdnUrl); XPathNavigator nav = doc.CreateNavigator(); string myNameSpaceNameLocalName1 = "xsi"; string myNameSpaceNameLocalName2 = "myxsi"; string myNameSpaceNameLocalName3 = "anythingHere"; string theOneImGonnaTry = string.Empty; theOneImGonnaTry = myNameSpaceNameLocalName1 + ":"; //theOneImGonnaTry = myNameSpaceNameLocalName2 + ":"; //theOneImGonnaTry = myNameSpaceNameLocalName3 + ":"; //theOneImGonnaTry = string.Empty; string myXPath = "//" + theOneImGonnaTry + "sdnList/publshInformation/Publish_Date"; Console.WriteLine("Let's try : " + myXPath); XPathExpression xpExpress = nav.Compile(myXPath); XmlNamespaceManager context = new XmlNamespaceManager(nav.NameTable); context.AddNamespace(myNameSpaceNameLocalName1, "http://www.w3.org/2001/XMLSchema-instance"); context.AddNamespace(myNameSpaceNameLocalName2, "http://www.w3.org/2001/XMLSchema-instance"); context.AddNamespace(myNameSpaceNameLocalName3, "http://tempuri.org/sdnList.xsd"); xpExpress.SetContext(context); XPathNodeIterator nit = nav.Select(xpExpress); while (nit.MoveNext()) { XPathNavigator nav1 = nit.Current.Clone(); Console.WriteLine("Publish_Date: " + nav1.Value); }
From: sloan on 22 Feb 2010 12:00
Thanks (again!) Martin. "Martin Honnen" <mahotrash(a)yahoo.de> wrote in message news:urt0t59sKHA.4796(a)TK2MSFTNGP02.phx.gbl... > sloan wrote: > >> <?xml version="1.0" standalone="yes"?> >> >> <sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >> xmlns="http://tempuri.org/sdnList.xsd"> >> >> <publshInformation> >> >> <Publish_Date>02/10/2010</Publish_Date> >> >> <Record_Count>4301</Record_Count> >> >> </publshInformation> >> >> </sdnList> > > The following should suffice: > > XPathDocument doc = new XPathDocument("input.xml"); > XPathNavigator nav = doc.CreateNavigator(); > XmlNamespaceManager mgr = new > XmlNamespaceManager(nav.NameTable); > mgr.AddNamespace("df", > nav.SelectSingleNode("*").NamespaceURI); > Console.WriteLine(nav.SelectSingleNode("//df:Publish_Date", > mgr).Value); > > > Or use LINQ to XML: > > XElement root = XElement.Load(@"..\..\XMLFile1.xml"); > XNamespace df = root.Name.Namespace; > Console.WriteLine(root.Descendants(df + > "Publish_Date").First().Value); > > -- > > Martin Honnen --- MVP XML > http://msmvps.com/blogs/martin_honnen/ |