Xml // Reading Element Value // Namespace issue // XPathExpression XmlNamespaceManager

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);

}












0
sloan5710 (6)
2/22/2010 4:27:51 PM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
2490 Views

Similar Articles

[PageSpeed] 23

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/
0
mahotrash (1777)
2/22/2010 4:45:20 PM
Thanks (again!) Martin.



"Martin Honnen" <mahotrash@yahoo.de> wrote in message 
news:urt0t59sKHA.4796@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/ 


0
sloan5710 (6)
2/22/2010 5:00:34 PM
Reply:

Similar Artilces:

XML Header
Hi, I have some XML doc loaded in a C# Web Service. XmlDocument doc = new XmlDocument(); doc.LoadXml(myDoc); Code Missing to manipulate header (I would like to replace or manupulate all these XXXXX): <mso-XXXXXX productversion="XXXXXX"> <mso-application progid="XXXXX"?> doc.Save(myNewDoc); Any idea on how to access and manipulate data within the doc header? Thanks vbdev vb6dev2003 wrote: > I have some XML doc loaded in a C# Web Service. > XmlDocument doc = new XmlDocument(); > doc.LoadXml(myDoc); > > Code Missing to manipulate header ...

CRM Mobile CA issues
Greetings, I am attempting to load CRM Mobile to a SBS 2003 server. The certificate issuance is causing my problems, (likely a user one). The way I understand it I need to create a Enterprise Level Root Authority using the server .local name and then issue a cert for the CRM site DNS name. When I run CRMMobIPCfg.exe I get several service failures. I can force them up in Service manager but the integration service terminates unexpectedly. Can anyone offer any direction; information on the net is sparse. ------=_NextPart_0001_2DECD42B Content-Type: text/plain Content-Transfer-Enc...

read only database
I usually update one of my users access database by accessing his folder and the database through the network connection. Since last week, the folder and the database are showing up as "read only" and i cannot add any queries or reports. Can somebody please help in correcting this problem? how do i make their database updateable?thanks Did you check the permissions on the folder to see if you have permissions to do what you want to do? It seems as if someone changed the permissions if you used to be able to make changes within the folder. I assume you can't add ...

Problem opening an XML file in Excel
I have Excel Professional 2003 (11.6113.5703) which has an XML conversion function but am not able to successfully open an XML document. I am working on a source file which was sucessfully opened by another part who also has Excel professional 2003. We both went through the same process, but she did not get the "Microsoft Office encountered errors when importing the following files" but I did. Her Excel file formats correctly using the XML tags as column headers, my file does not. Here is how I opened the file: >Opened as XML list, OK. Then a message, "The specified...

issues with 4.2.2.2 giving wrong IP address for DNS
I have been plagued by a single server in the west that has had trouble sending email to an external client. The telnet tests worked fine, the connections from everywhere else work fine, but, it appears that the public DNS server of 4.2.2.2 was giving bad IP address information. The domain name we were sending to (MX record) has not changed in 11 months, but the 4.2.2.2 DNS query was pointing at the wrong IP address. I removed all FORWARDERS in SBS 08, restarted the transport services, and email works fine now... I've never had problems with 4.2.2.2 before. Just thought I ...

Issues with demoting exchange server? #3
Hi, It seems that the previous administrator was not succesful in fully promoting a sever to a DC. We are supposed to have two DCs (CC7=First DC, CC5=DC with Exchange server). There have been continuous replication problems from cc5 to cc7. AD doesnt consider cc5 as a DC and cc7 is the only DC in Domain Controllers group of AD. Manual replication between two DCs results in 'no endpoints' error. AD is unavailable several times on both DCs. Object created on a DC is not replicated within two (!) days on second DC. Clearely cc5 can not communicate with cc7 the way it is supposed to. T...

Read XHTML into XML
Hi all, I need to read/parse XHTML aspx pages and look for certain tokens and content. How can I use a XmlTextReader for this? If not, any other ideas? Thanks in advance, JA Reyes. Jose Antonio Reyes wrote: > I need to read/parse XHTML aspx pages and look for certain tokens and > content. How can I use a XmlTextReader for this? If not, any other ideas? If the pages are well-formed XHTML then it is possible to use XmlReader (in .NET 2.0/3.0) or XmlTextReader (in .NET 1.x) to parse the XHTML documents. You can also use the other XML APIs .NET provides so using XPathNavigator and/o...

How to invoke the xml editor?
I installed vs2005 on a new xp pro machine, and installed everything except C++. When I click on a file with a ".xml" extension it is opened with IE. When I click file/Edit with XML Editor, it opens it with Notepad. If I click File/Open With and select XML Editor, it opens it with IE. So, where is the xml editor? Can it be invoked from withing VS? Where? Bill * billsahiker@yahoo.com wrote in microsoft.public.dotnet.xml: >I installed vs2005 on a new xp pro machine, and installed everything >except C++. When I click on a file with a ".xml" extension it is >opened ...

Stubborn Link Data/Value Problem
Hello, I am having a problem with a link in one of my Excel worksheets and I'm hoping that someone can help me with it. Here's the situation... I have two separate workbooks that are connected via a link such that an update in one workbook is automatically (and immediately) reflected in the second book. There is a cell (say A1) in the second book (the destination) that contains a link with the following format: =Excel.Sheet.8|'C:\Folder\Source.xls'!'!Sheet1!R2C1' [I created this link by copying the source cell from the source workbook and pasting (using Past...

How to stop screen so i can read blue screen message
Hi, I have XP pro and it has started to keep on rebooting at start up with a flash of a blue screen message which I can't read as it is so fast. It boots up with the Windows logo screen and the blue lines showing it working for a few seconds but then it goes black and then the blue screen flashes and it then reboots to the same thing again. How can I read that blue screen and how can I see what it wrong and fix it. Thanks oz "rock" <1940@pobox.com> wrote in message news:#bLvKtrpKHA.5760@TK2MSFTNGP02.phx.gbl... > Hi, > > I have XP...

Excel reading tab delimited import text
I have imported some values via tab delimited text file. Excel prints all the values but only reads some of them Please help. ...

domain admin can read all mailboxes.
good afternoon our setup is sbs 2k3 with exchange 2k3. just found that on owa, the domain admin can open everyone's mailboxes. i don't want it ( me ) too be able of doing so. what permissions must i set, under aduc, exchange advanced properties, must i set on deny so that it becomes impossible, without breaking exchange. i am using exmerge daily to export mailboxes to pst files. thank you Pedro Leite Check you don't have full mailbox access granted, and ensure Send As and Recieve As are denied for Domain Admins by default. Oliver thank you "Oliver Moazzezi" ...

Moving a line chart data point revises data table value in Excel '
I recall a capability I used with Excel years ago that enabled me to create a line chart from a data table, then move the plot points on the chart to "smooth" the line, and the source data chart value would change with the corresponding new plotted point. I'd like to use that capability with Excel 2007 but can't find to activate it. Any power users know how? Thanks! Ed Ed, Microsoft decided that users didn't need that feature. More than likely, they were tired of support calls asking why the values changed when one dragged a data point...... No really, they took i...

Offline synch using XML web-services
This is a bit long-winded. Can somebody read thru these 2 use cases, and tell me if it is feasible ? :- Use Case : Replication before Offline Synch Scenario 1 : Oracle Server , MSDE client 1. User clicks on 'Go Offline'. System creates a MSDE SQL database in laptop (client), gets the schemas of the tables in Oracle, creates corresponding tables in client's MSDE SQL (mapping Oracle datatypes to SQL datatypes). 2. Offline replication program reads in the tables of the server into a typed datasets, converts the typed datasets into XML, and stores the XML in the client. (XML serial...

XML Serialization ... how to truncate <?xml...
While xml serializing object how can I truncate <?xml ...?> this line alone? I need to send the serialized xml data to a web method ... since SOAP already have <?xml tag there was some confusion Any help would be greatly appreciated Thank you Regards Raj Raj wrote: > While xml serializing object how can I truncate <?xml ...?> this line alone? Serialize to an XmlWriter created with XmlWriterSettings where you set OmitXmlDeclaration to true. -- Martin Honnen --- MVP XML http://msmvps.com/blogs/martin_honnen/ ...

LDAP Directory issue
I use Outlook and have added several people to my Contacts list. I composed a note and attempted to select the "To:" button so that I could choose the addresses, I get the following Error Message: LDAP Directory is unavailable (52) The Contacts List is accessible so I figured that my setting are wrong, However, I am unable to determine the correct settings. Can I get some help on having my Contact list as the default file for when I am composing a note? ...

rollback combobox to 'BeforeUpdate' value
How do I set a combobox to a previous value? When finished updating a particular record the user selects an identifer in a combobox to advance to the next desired record. I use the BeforeUpdate event of the combobox to ask a yes/no question. If NO I want to stay with the current record and this works. the problem is that the combobox has been advanced to the new selection and stays there. Now I have the identifer for one record in the combo box but the form is still (as it should be) on the current record. I need to roll back the combobox to what it was before the update bu...

Office 2004 printing issues: Apple, MSFT, help!
MSFT MVPs: many folks are reporting issues printing from MS Office 2004. The main symptoms are blank pages in the print preview or quick preview / print dialog, when in fact the pages contain content. Blank pages then are printed from the printer. Xerox, Samsung, Lexmark, and other printers seem to be affected. Complete re-installs of 10.5/.1 // Office // print drivers don't fix the issue. Printing works for every other app outside of Office. Is this an Apple problem? A MSFT problem? A print driver problem? HELP US!!!!!! Thanks! Answered your other post On 27/11/07 11:14 AM, in ar...

Free map XML web service?
I want to create an XML web service that allows a user to enter two U.S. addresses, and be able to obtain step-by-step driving directions. At first I was hoping that Yahoo or Mapquest would have a web-based API that I can use, but I can't find any APIs for them. PLEASE let me know if they (or any other map sites) have these APIs. So my second alternative is to find a free XML web service somewhere out there that can provide more or less the same functionality. That way, I can still create my custion XML web service, and have it call the 3rd party web service to do the job. Microsoft&...

String to xml document
Hi there, I was hoping someone can help me with a little problem I can't seem to find any answers to. I've got a form wich will be posted. On receiving the post I would like to parse a formvar to a xml document. The contents of this formvar (string) will be a valid xhtml document. Can someone give me a startertip (or more :-)) on how to achieve this? tnx in advance, James van der Veen Sjeems wrote: > I was hoping someone can help me with a little problem I can't seem to find > any answers to. > > I've got a form wich will be posted. On receiving the p...

Outlook 2007 Performance Issues
I recently upgraded my Windows from XP too 7 and I am having major performance issues with my outlook 2007. My pst file is about 2gb's. It takes around a minute for outlook to open and after it does it takes 20 to 30 seconds to get a response when do anything either from clicking on an email, moving it to another folder and writing and email. However I did notice that when I type an email without opening outlook 2007 everything looks fine. It's when I open the program itself I have the performance issues. I have a Core 2 Duo laptop with 4 gb ram. I checked the web and ...

storing value in form field
Hello, I want to get values from a pop-up date form to insert in a text box on another form. Problem is when I close the date form, the value that the text box was referring to is gone. Is there a quick way to save the value in the main form until new dates are typed in? Thanks You should be able to hide the form rather than close it. "ryan" <ryan@discussions.microsoft.com> wrote in message news:49EA576C-544E-4A18-B4FB-4F7AF6D2951E@microsoft.com... > Hello, > > I want to get values from a pop-up date form to insert in a text box on > another form. Problem...

Excel does not display entered values correctly
I am currently running Excel 2000 on WinXP Pro. Excel does not display the correct values in cells when they are typed. The program arbitrarily assigns a decimal value to whatever number is typed. For example, if "1111111111" is entered, it is displayed as "11111.11111" I have gone through cell formatting, detect and repair and other options, but none have worked. The program use to work fine. It started this recently and it won't go away. If it helps, when a number is typed in another program, i.e. notepad, and copied and pasted into Excel the value displ...

Beginner of XML
I am a beginner of XML. I want to group some files together with a xml file. (it includes a text file, and an image file and a video file) Can someone show me some guidelines? Please help. ...

How to delete an unsent "Read Receipt"
I accidently told Outlook to send a "read receipt" for a message that was received in an email account that I've deleted from Outlook. Outlook is now constantly trying to send the receipt, but it won't go through since the email account is not in outlook anymore, so a dialog box is constantly popping up asking me for proper user id and password. I don't know how to delete the unsent "read receipt" message, as I can't see it anywhere. Thanks much for any help ...