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
2564 Views

Similar Articles

[PageSpeed] 47

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:

Office read only in outlook only
I am having a problem with Outlook 2002 and a user profile. The user loggs into locations and everwhere she loggs into the Outlook gives her office read only files. She can send the file and it becomes read only. She can send to any user possible but the file comes up as normal. Any user can read the file as normal, but she cannot. she opens the file seperatley it will read regular, but when opened through Outlook it is read only. Please Help!!!!!!!!!!!!!!!!!!!!!!!! ...

Prevent reading extensionAttribute
Hi everyone, I hope this is a simple question, even if the the How-To might be difficult. I'm trying to prevent users other than a specific universal group, as well as domain admins, from reading the data in the extensionAttribute1, and extensionAttribute2 attributes on the user property in AD that Exchange 2003 puts there when you install it. The reason is that we have some sensitive data in that field, and as it is now, anyone (Authenticated Users) can query using LDAP and suck that information out, which happens to be priveleged HR information. Anyway, is there a way we can den...

how do i change the default value of measure from points to inche.
how do i change the default value of measure from points to inches when setting the width and hight of cells? You don't. Excel uses only points for these measures best wishes -- Bernard Liengme www.stfx.ca/people/bliengme remove CAPS in email address "yoyo4u" <yoyo4u@discussions.microsoft.com> wrote in message news:33420157-6E05-4A55-9003-088D731E495E@microsoft.com... > how do i change the default value of measure from points to inches when > setting the width and hight of cells? yoyo Row heights are measured in points. There are 72 points to an inch. Th...

failure of Open XML 1.1.4 to work
Version: 2004 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Previous versions of this worked fine. This version comes up with comment &quot;not enough memory&quot;. As I have 2mg Ram this seems strange. Any help appreciated. I'm afraid you haven't provided sufficient information for anyone to go on, especially since that message quite often has nothing to do with memory. For any questions of a technical or performance nature it's important that you specify your current update level of OS X as well as for Office. Exactly when does this messag...

Query to Mail Merge issue
I am running Access2003 on WinXP SP2. I send out dues letters for members of a retiree group. When I try to do a mail merge to my Dues Query all goes fine except the telephone number in the query which is (555)555-1234 comes out 5555551234. If I copy the query into an excel spreadsheet and use that as the datasource for the mail merge it works fine. Is there something I need to do to format the query so it merges with the right format. Thanks Billa In the query format the field to include the parentheses and dash. In query design view, instead of the field name put an expression alo...

Chart to show Portfolio Value over time?
I'm using Money 2003. I would like to be able to see the $ value I have in my portfolio over time. So that I can weep. Money does have a chart view that allows you to see the PRICE history for a given stock over time - but not the dollar value of your investment in the stock over time. In fact, I can't seem to find a view at all that shows you the net value of your portfolio/individual stocks changing over time. The best I've been able to do is to use the "Net Worth" report and unselect all the other accounts. This has insufficient granularity (months instea...

Exchange OMA error message. Please read...
When our i-700's with Pocket PC 2003 attempts to access our mail exchange server, all we receive is the following error message: " A System error has occurred while processing your request. Please try again. If the problem persists, contact your administrator. Home" mail.domain.com/exchange works fine though. We've installed the service pack 1 for exchange 2003 to attempt to fix this issue and are having the same results. Does anyone care to share how they may have overcome this exchange problem with their compatible PDAs and Exchange's OMA (Outlook Mobile Access...

Insert empty numeric value
Dear all, In VB, I have three textbox which are amount1,amount2 and amount3. After user enter the value in the textbox, I will insert the value into Access table. The table have three columns amount1 , amount2 and amount3, and all are nummeric Type. However, if the user do not enter any value in textbox . The insert statement will become as follows: Insert into table1 (amount1,amount2,amount3) values (,,) Then access complain that there is syntax error in insert statement. Does that mean I cannot insert empty value for the numeric value in access.? How to solve this problem. Than...

Subtracting value from main form
I have a borrow module which will alow user to return item separately. So, I have get the structure of returning it separately. In my main form is the borrowing item, with the loaned quantity and the owed quantity (will be calculated). In the subform, there is the returning transaction. User will need to key in the quantity returned and it will be automatically deducted from the quantity owed. But how am I supposed to get the quantity deducted while it 1 is in main form and the other is in subform? -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-fo...

How do I find a value on a line?
I made a line graph of data to use as a calibration. I know the y value and I want to find the X value. Is there a way that I find find this specifically on the line without using a trendline formula or guessing by looking at the gridlines? The only way to find a specific value is to use the formula. Rgs, Bou If you can accept piece-wise linear interpolation, see Interactive Chart http://www.tushar- mehta.com/excel/software/interactive_chart_display/index.html -- Regards, Tushar Mehta www.tushar-mehta.com Excel, PowerPoint, and VBA add-ins, tutorials Custom MS Office productivity s...

Query Criteria issue
Access 2003: Trying to create a demographics report that will allow the following. Gender example On the form "frmStudentDemographics" is a drop down field named "cboGender" When the user makes a selection "Male" a report is generated for only Males. But in some cases the user will want to run a report for both (all) genders. So I added a check box next to the drop down named "ckAllGender" My idea is that if that box is checked then the report will show information on Both (all) genders. So in the query that generates the report, in the c...

Replace null value with the previous value?
I have a database that was just imported that has approximately 388000 records. The problem is that there is information about a person in multiple different records but the name did not come across with each record. (So I have 10 records with information for a certain name, but the name only appears in field 1 of the first record and not the subsequent 9, etc.) I need to create a query or expression that will fill field 1 with the preceding value if it is null. This way I will have all the information for field 1 in a manner that I can link and combine data. Simply I need to fil...

Calculating values for empty cells.
Hello. I have a very simple problem that I cannot find the answer to. I have data in two columns, some of the data in one of the columns is missing and I want to automatically extrapolate what the data should be based on the trend. How can I get Excel to fill in empty values without overwriting the known values. Below is a sample of my data. 1500 1600 1700 1800 4000 1887 5700 1900 5500 1910 7300 1912 8100 1920 8800 1926 10100 1930 11900 1936 12200 1938 -- Ryan Taylor rtaylor@stgeorgeconsulting.com Not sure what yo...

Default value for custom field?
How can I populate a custom field with a default value? Specifically, I have a custom field "DisplayName" associated with the Quote Detail object. I want initially to populate this field with the value of the product name field when a new product is added to a quote. The user can then edit the DisplayName custom field if desired. The DisplayName custom field will be used as the product name on a Crystal Reports quote form. ...

xml & script languages
I need some literature of integration of XML with script language! Any links? Thanks! mgalesic wrote: > I need some literature of integration of XML with script language! Wich script language? Do you want to use it on the Server (PHP, JSP, ASP, ASP.NET, Perl, Ruby,...) or on the Client (JavaScript & XmlDOM)? -- Pascal Schmitt Pascal Schmitt wrote: > mgalesic wrote: > >> I need some literature of integration of XML with script language! > > > Wich script language? > > Do you want to use it on the Server (PHP, JSP, ASP, ASP.NET, Perl, > Ruby,.....

XML extension cause file download loop
All, Suddenly. When I click on an XML file expecting it to open in IE, it begins the file download screen, then attempts to open IE. These 2 screens flash in an endless loop. What's up with that??? Any ideas are welcome! Thanks -- Mike Gasperino Sr. Software Developer office: 919-807-2310 Helpdesk: (919)807-2300 doa.helpdesk@ncmail.net **************************************************************************** E-mail correspondence to and from this address may be subject to the North Carolina Public Records Law "NCGS.Ch.132" and may be disclosed to third parties *****...

How to create an XML document with XmlTextWriter?
Hi, I'm trying to create an XML document with XMLTextWriter but I want to store it in memory (not write it to a file like so many examples do). I tried the following but with no luck. Should I not user this object for this purpose? Thanks, Dave MemoryStream stm = new MemoryStream() XmlTextWriter writer = new XmlTextWriter(stm, System.Text.Encoding.UTF8) writer.WriteStartDocument() writer.WriteComment("This Is A List of My Books") writer.WriteStartElement("MyBooks") writer.WriteStartElement("Book") writer.WriteAttributeString("ISBN", "1861005...

document elements (et. al.) toolbar in Word 2008
I may be in the minority here, but I happen to love the Ribbon UI on the PC version. It can collapse down to nothing when needed, and expand out (and even be customized I hear in 2010). That's on the PC. On the Mac I want a Mac-like UI. The Document Elements bar is so out to planet Jupiter on both functionality and as an accelerator into functions writers need most that it's laughable it made it into a release. (I'd much rather have a Mac ribbon that the Document Elements toolbar since at least the ribbon exposes USEFUL functions.) I mean, really =97 how many UPS or Del Mon...

Security Issue after upgrade to 2003
I upgraded a client to Office 2003. He then received an email with excel attachments. When he tried to open the attachment, excel gave him the error: "Cannot open file. Your antivirus has found a problem with this file" I forwarded over to my email (we are on the same network) and while I had to enable a macro, it opened fine. I downloaded both files and sent it back to him. Same message. I then disabled Norton Enterprise so that he has no AV running and tried to open it again. Same message. Everyone else I have sent it to has no trouble, and this is occurring only since t...

Limit value for Storage Groups
Hello, we are running Exchange 2003 Standard edition. The limitation on Storage Groups for e.g. "Issue warning at(KB)" is default set by 2097151. I read that i can change this value by using ADSI Edit and edit the values mDBOverHardQuotaLimit mDBOverQuotaLimit mDBStorageQuota But each of them has a lots of values and i could not find out which one i have to edit. Could you please give me an advice? TIA helpi If you want to set a value bigger than 2GB for all users on an entire mailbox store, bring up the properties of that mailbox store in ADSIEdit and set the mDBStorageQuot...

custom report not showing values
I have uploaded a very simple SRS report that includes "out of box fields" and custom fields. When I run the report, instead of my custom field values showing in the report, I get the id of the value. What am I missing? -Melissa ...

Value shading map issue
I am creating a map in Excel using MS Map to show a US map. When using the value shading function, I can't figure out how to keep the states that have no values blank. What happens is the key shows a range of values (0- 10, 11-20). I need the states that have NO value to have their own color (or lack of color) and I am not sure how to do this... Any suggestions? Chris In your data, you could calculate the state name in a new column, and use the calculated name in the map. For example, if state names are in column D, and values are in column E: =IF(E2=0,"",D2) Th...

Extract Values from a Column
I have a worksheet used to track time spent on various jobs. One column is for the JOB # while others are for descriptions, etc. I would like to have formulas or possible a macro (if necessary) to sum the time spent on different jobs. For example, say (for simplicity) that each row is equal to 1 unit of time. Then in this row I put 342 for JOB # 342. After the entire day I have worked on say 5 different jobs. I would like to have a cell that says "Total time spent on Job # 342" and then next to it a formula that would look at the column and count all the values that are equal t...

read receipt in exchange 2003
Hello All, Does anyone know how to block read receipt from leaving exchange org without using third party tool. We are looking to block read-receipt from going outside our network. By the way, we are using exchange 2003. Thanks in advance Hi, Unfortunatly because it is actually the client that sends the receipt you can not block them for user accounts. But they can be blocked for Distribution Groups, by selecting the option in the distro group "Do not send delivery reports" Regards -- Ben Hoffman MCP (Win2000 Pro/Server & Exchange 2003 Admin) http://www.ExchangeIS.com...

Help: Seting the value of another cell with a formula
I know that there's a way to make a cell a particular value based on the entries of a range, or array of cells, but is it possible to do the reverse, using only one formula in a cell. Here's what I'm trying to do: What I would like to do is set one of a range of cell to have a value based on the value in A2. Example: Cell A1 has a value of 2, A2 has a value of 2007. I want A10 to equal A1, i.e.:2 Cell A1 has a value of 2, A2 has a value of 2008. I want A11 to equal A1, i.e.:2 Cell A1 has a value of 2, A2 has a value of 2009. I want A12 to equal...