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
2791 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:

Tr again: Modifying Calendar's "reading" view in 2003
In Outlook 2003, Calendar allows me to show the event's content in a "Reading" pane at the right or bottom of the calendar. But the layout of the Reading pane wastes a lot of space. Can its layout or content be modified? No, the content can't be modified. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Author, Google and Other Search Engines (Visual QuickStart Guide) Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com...

Change <value> in outlook
We Have Exchange 2003 sp2 and outlook 2003 sp2. I have renamed 2 AD accounts. Changed all display names, accounts name etc with the 2 accounts. When users open outlook and send to renamed account they see account name <oldaccountname> in the autocomplete list. I have cleared nk2 files in profile , but it still shows newaccount <oldaccountname> in drop down box. Is there a way to edit the <oldaccountnam> value of the renamed account? I tried the same thing with a freshly loaded PC. with the same result. Thanks You can delete the nickname files. -- Ed Crowley MVP - E...

SafePay Issue
Does anybody have a suggestion on the following issue? My customer uses SafePay. They printed a batch of checks. The checks jammed. We allowed Duplicate Checks on that checkbook and reprinted the checks on the good checks. We processed SafePay like normal. Now the bank is generating an exception report because we send duplicate check numbers to them, one with a void and one without. Any suggestions on how this should be handled? Maybe I should just push harder for them to print on blank check stock with Mekorma. ;-) -- Japheth Nolt Microsoft SBF Specialist Landis Computer www.landiscomp...

Date format issue when submitting from a userform to a spreadsheet
Hi, I have a userform that I've generated which routes dates onto a spreadsheet based on the users input. I am having a bit of a frustrating time with the dates, it would appear that in the process of moving the date from the userform to the spreadsheet some dates are switched/transposed. I'll give an example. If someone enters 09/02/2004 on the userform excel seems to look at 09 and assume it is a month and transposes the dates to 02/09/2004(this does not appear to be a US/UK format issues as I have already gone down that road). However, if the date 13/09/2004 is entered i...

Sorting Alphanumeric values in a text field
I'm using Access 2003 for a database for my company. I have a field in a table that has both text and numbers. They are part numbers, for example 21BC124. I kept the field as text because of the text with in the numbers and didn't figure that a numeric field would alow the text. In my part numbers table it sorts correctly (first by number then by letter then by number again), but in my reports and queries there are a few number that sort in the wrong place. Like this... 20D10-3 21BC123 21BC128 22D10 25TD47 21FA101 21FA200 25FA203 38FA601 21FP604 38WS100 I can't quite f...

Count unique values
Hi, I've some problems to count unique items (Invoice #) in a pivot table. There is the default solution "Add-a-new-calculated-column" as mentioned on http://contextures.com/xlPivot07.html#Unique, but in my case, it doesn't works. My problem is, that these values aren't in a Excel worksheet; it's a external data source - a SQL-Select via DAO/ADO. Because of that, I don't have the possibility to add a new column. Another reason is, that the pivot table should always be dynamic: Group over this field, group over another field - and always show the number of unique...

Passing a variable as a parameter value instead of a literal string to a child report?
In the Navigation section (Jump to URL), I have the following: ="javascript:void(window.open('http://localhost/reportserver?/Reports +Folder/My +Report&rs:Command=Render&StartDate=01/01/2009&FinishDate=12/31/2010'))" This works as designed - a new window pops up with the "My Report" report and the StartDate and FinishDate of 01/01/2009 and 12/31/2010 are passed respectively. But what is the syntax for changing the "01/01/2009" and "12/31/2010" to variables that point to the StartDate and FinishDate parameters of the PAR...

help with preview pane and "read receipts"
Hi, I'm new to Outlook administration. We run Outlook on a server wit Exchange. Is it possible to setup Outlook on client PC's so that th preview pane cannot be activated by individual users? Also, can the blocking of "read receipts" by individual PC users b prohibited within Outlook, at the server level? If not, is there an other way to do it? My reasoning for wanting to do the above two things is to make th "read receipts" function work more effectively. Thanks for your suggestions. : ----------------------------------------------- ~~ Message posted from h...

Contract Administration/SOP Issue
When the inovoice is transferred to SOP I am not getting the item description to populate correctly. Item number populates the Item description field. It needs to pull the item description from the contract line. I can work around this temporarily by modifying the invoice to print the item description from the item maintenance card. Any suggestions to have the SOP window display properly Accounting , This is how it's supposed to work. If you would enter in the item in sop as the normal item from contract maintenance, you would be selling one of these items from your inventory ...

Top values
Dear friends, need your help again please. I have a table: tbl_Plots (PlotID is the primar key - number byte) and tbl_Data (ID is the primary key - autonumber, PlotID related to tblePlots, height - number byte and diameter - number integer). I need to select the 20 bulkiest trees of each plot, i.e. having the biggest diameter. Also, perhaps in a plot less than 20 trees will be present so I will need all of them. Any suggestions? Thanking you in advance, GeorgeCY hi Geroge, George wrote: > I have a table: tbl_Plots (PlotID is the primar key - number byte) and > tbl_Data (ID is...

question about using the correct schema namespace
I have a Access/VBA client that exports xml to the local drive, then posts it over http to an aspx page. The aspx page consumes it, then builds itself based on the xml data. This works find only if I first mannually change the root entry's namespace url as shown below. Does anyone know how I can get the two (cleint xml export and aspx xml consume) to work together with the appropriate namespace? Here is the top three lines of my xml export (prior to mannually changing it): <?xml version="1.0" encoding="UTF-8"?> <root xmlns:xsd="http://www.w3.org/200...

Using Sumproduct when some of the values are null
I am having a problem using Sumproduct when some of the cells have null values. I will try to describe the problem. Please let me know if you need additional information. Do any of you have any ideas of how I can fix that sumproduct to get it to work? A B C Row Trend Claims 5 Emergency $0 6 Emergency $0 7 Emergency $0 8 Emergency 81.68% $24,444 9 Emergency 35.00% $164,758 10 Emergency 35.00% ...

How to get XML data out of an XML file
I am trying to retrieve the Parameters first or second (0, 1 ,2) node from the following XML file: <?xml version="1.0" encoding="utf-8" ?> <Robot xmlns="http://tempuri.org/RobotDefaults.xsd"> <Parameters> <Name>Decker</Name> </Parameters> <Parameters> <Name>A</Name> </Parameters> <Parameters> <Name>B</Name> </Parameters> </Robot> There will be more data than just a name for each Parameters node. Here is my code: Dim node As XmlNode = xmlDocument.SelectSingleNode("/P...

Any XML Naming Conventions?
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET csgallagher@REMOVETHISTEXTmetromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ clintonG wrote: > Does the use of DTD, X...

XML as a database that can be written to
Has anyone had any luck using XML as a database that not only can be read, but also written to? What features are available in .NET to do this? I need to do all of this from a Web based application. Am I thinking File System Object? Thanks. mjc wrote: > Has anyone had any luck using XML as a database that not only can be read, > but also written to? That's doable, but you better use real database instead. XML wasn't meant to be used as a database, it's just a data format. -- Oleg Tkachenko [XML MVP] http://blog.tkachenko.com Yes, it can be done and I think its an id...

Problems with loading xml from XmlTextWriter
I use an XmlTextWriter to generate xml data. The declaration is: System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(sb); XmlTextWriter w = new XmlTextWriter(sw); And I write to it using .... w.WriteElementString("foo","bar"); When the creation is done, I need to load it into DOM to select an XmlNode or an XmlElement to apply to another method (which I cannot control). I thought I could do it with: XmlDocument xDoc = new XmlDocument(); xDoc.Load(sb.ToString()); XmlNode node = xDoc.SelectSingleNode(&...

Issues Template
Hi i have downloaded the issues template to work in a vet office, but am not sure what the Add related issues is for on the Issues details form. i jus want to know what it does and its benefits It's unlikely that you'll get any help that requires anyone to download and analyyse someone else's guickie application. You'd do better to use the sample databases and tools that are provided with Access and ask questions based on them, instead. HTH -- -Larry- -- "Amin" <Amin@discussions.microsoft.com> wrote in message news:1B20F4FC-2696-4724-8615-49...

Getting Cell Value from the Concatenate formula
I built a concatenate formula that returns the following result: =Jul! $D27 I am looking for the cell contents of Sheet: July Column D Row 27. I tried to use offset, but I am stumped. Can I add something to the front of the concatenate to not only build the reference to the cell, but also return the value instead of the =Jul!$D27 ? Thanks John =indirect(yourformulahere) Don't include the equal sign in your formula. And match the name correctly (Jul or July???). Depending on the name of the worksheet, you may need to have a string that looks like: 'Sheet 99'!d27 =indirec...

Media Center Screen Scaling Issue
I have a desktup running 2 screens. My main runs at 1650x1080 and the second is 1280x1024. I always run my media center in a window streched to the max size on the second screen. The problem is that I can't get it to go to 4:3 in windows 7. In Vista I was able to do this by dragging the media center so it was overlapping both screens and then maximising it. That used to make it 4:3 when it was restored but that doesn't seem to work in vista. The first week I was using 7 I did get it to behave but I have no idea how, now it's stuck and I can't seem to fix it. ...

ComboBox passing values from selected
In my form (Items_frm) I have a combo box (Itemcbx) that displays 4 columns (Item, Makebuy, Revision, IsActive) but is bound to the first column. However, I would like to pass the other values from the remaining columns to other fields in my form after the selection. Is this possible? Can someone provide a sample code? You can use the Column() attribute. Me.Itemcbx.Column(1) etc. Note that the index for combo box columns is zero-based so 0 is the first, 1 is the second and so on... Steve "Angel G" wrote: > In my form (Items_frm) I have a combo box (Itemcbx) that displ...

Deleting Unique Values
How do you delete unique values in a column? I need to filter 7500+ rows to only display duplicate values. The VBA code below will delete the entire row when the value in col. "A" will be uniqe. Consider to make a copy of your entire sheet in order to test the code and see if this is what you need. ------------------------------- Sub Delete_Uniques() LR = Cells(Rows.Count, 1).End(xlUp).Row For R = LR To 1 Step -1 If Application.CountIf(Range("A:A"), Cells(R, 1)) = 1 Then Cells(R, 1).EntireRow.Delete End If N...

Making words in a list a value!!
I am trying to create a list that will generate a value in another column once that word or phrase is chosen. Is this possible and how. Exp. In the drop down list I would chose Product, then in the price column the price automatically appears. brco1, Much better than I could ever explain the process. Here's exactly wha you need. http://www.contextures.com/xlFunctions02.html HT -- Case ----------------------------------------------------------------------- Casey's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=454 View this thread: http://www.excelforu...

Work spreadsheet issue
If I have multiple rows of data on a worksheet and on another worksheet I want various counts of rows that have certain things in common, how would I do that? Thanks. -- jenrenea ------------------------------------------------------------------------ jenrenea's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=23098 View this thread: http://www.excelforum.com/showthread.php?threadid=373900 Can you provide a sample of your data along with your expected results? -- Domenic ------------------------------------------------------------------------ Domenic's...

Getting right date value
I setup my DTPicker control to be used only as a date control, yet I'm noticing that sometimes it will give back a date AND a time all in the same "value" variable. Since it appears that a variable of type "Date" can give back both a date and time, how can I eliminate the time half of a date value??? I might not be able to exactly control the DTPicker control to give me JUST a date, so I'm just curious what to do if it gives me back both a date & time. thank u Hi, Try this : Dim x as date x = cdate(clng(DTPicker1.value)) &qu...

find match then change cell value
In column A I have cells filled with text and in column B I have cells filled with numbers. I need to check if the number in cell C1 equals any of the numbers in column B. If a match is found then I need to change the text in column A to CBO. e.g. Column A Column B Column C aep 5 7 apa 0 gci 59 xto 5000 xle 7 oih 253 ed 8 Since the cell C1 = 7 equals the 7 from column B, I need to change the data in column A from xle to cbo. Is this possible...