Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / May 2008

Tip: Looking for answers? Try searching our database.

XML namespace and LINQ to XML

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alexander Wykel - 09 Apr 2008 23:00 GMT
I have an application where I would like to use namespaces in my schema

An example of the Xml that uses this namespace:

<?xml version="1.0" encoding="utf-16" ?>
<Project xmlns="http://P2SWorld.com/AutoDeveloper/AutoDoc/Project"
name="Quick Change Menu Item" LastModified="04/09/2008" Version="1.0.0">
 <Tabs>
   <Tab name="Craft Codes" />
 </Tabs>
</Project>

The following code fails:

XDocument projectFile = XDocument.Load("c:\project.xml");
XElement tabs = projectFile.Element("Project").Element("Tabs");

This code worked before I added the name space to the fiels.  I have spent
the two hours looking for the reason but can’t find a solution.  What do I
need to do to enable namespaces in my XML documents?

 Thanks in advance

Signature

Alexander L. Wykel
AW Software Works

Wen Yuan Wang [MSFT] - 10 Apr 2008 04:19 GMT
Hello Alexander,

What you need is to define a namespace for LINQ to XML.
Please try the following method.

           XNamespace ns =
"http://P2SWorld.com/AutoDeveloper/AutoDoc/Project";
           XDocument projectFile = XDocument.Load("XMLFile.xml");
           XElement tabs =
projectFile.Element(ns+"Project").Element(ns+"Tabs");

Hope this helps. Please let me know if this works fine on your side. We are
glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Alexander Wykel - 15 Apr 2008 08:59 GMT
Thank you Wen Yuan,

 This did answer my question, but it took a lot of work to implement.  My
original code did not use any namespace so I had to modify and test some 3000
lines of code to make sure this would work.  I think it would be easier if
there was some was you could subclass the XElement, provide your own
namespace and then global search and replace would be much easier.  

Then you could do this:

Where you had this:

guiNodes = xdoc.Element(GlobalArrtib.tabNameSpace +
"AutoDoc").Element(GlobalArrtib.tabNameSpace +
"TabPage").Element(GlobalArrtib.tabNameSpace + "DetailView");

replace with this:

//Global definition of your Element types...
ElementAutoDoc ad = ElementAutoDoc();
ad.NameSpace = GlobalArrtib.autoDocNameSpace;

ElementAutoDocTabPage adtp= ElementAutoDocTabPage();
adtp.NameSpace = GlobalArrtib.tabPageNameSpace;

ElementAutoDocDetail add = ElementAutoDocDetail();
add.NameSpace = GlobalArrtib.detailNameSpace;

the you can have:

//code implimentation
guiNodes =
xdoc.ElementAutoDoc("AutoDoc").ElementAutoDocTabPage("TabPage").ElementAutoDocDetail ("DetailView");

Which is much cleaner.

The current version cause a lot of string concatenation.

Is there any plan on implementing LINQ to XML the way it's done in VB with
C#?  That is much cleaner.

Alexander L. Wykel
Signature

Alexander L. Wykel
AW Software Works

> Hello Alexander,
>
[quoted text clipped - 37 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Wen Yuan Wang [MSFT] - 16 Apr 2008 05:44 GMT
Hello Alexander,

Thanks for your reply, and posting back your solution.
Extending the XElement class is really great idea for default namespace. I
think other guys will get benefit from your reply.

Please understand this is the first version of LINQ to XML. Thereby, we are
looking at continual improvement. But I'm not sure if C# team has a plan to
support XML as the way in VB. I suggest you can submit this feedback to our
connect feedback portal. This kind of feedback that let product team know
what things you're trying to do, that we haven't yet exposed for you. Our
developer will evaluate them seriously and communicate with you directly on
the issue there.
http://connect.microsoft.com/VisualStudio/

Improving the quality of our products and services is a never ending
process for Microsoft.

Thanks again for your suggestion and feedback. If you have any more concern
or there is anyting we can help with, please feel free to let us know. We
are glad to assist you.

Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Roshawn - 18 Apr 2008 02:20 GMT
From what I've experienced, importing a namespace works.  Try this:

VB.NET    Imports xmlns="http://P2SWorld.com/AutoDeveloper/AutoDoc/Project"

C#.NET  using xmlns="http://P2SWorld.com/AutoDeveloper/AutoDoc/Project"

Your code should work as though you didn't have a namespace.  You need not create an
XNamespace object for it to work.

Cheers :)
Wen Yuan Wang [MSFT] - 18 Apr 2008 06:31 GMT
Hello Roshawn,

Thanks for your great suggestion. This is really the best way to import
default namespace in VB.net project. But, as far as I know, C# doesn't
support such default namespace. We cannot import namespace by Using
Statement in C#. I think this is the feature Alexander requires in C#
project.
In C#, we have to use XNamespace to import namespace. Alternative way is to
extend XElement as Alexander suggested.

If I misunderstood anything here, please don't hesitate to correct me.
Thanks in advance.
Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Alexander Wykel - 08 May 2008 17:43 GMT
Do you see any design problems with this, I wil impliment this if you think
it will work.
Signature

Alexander L. Wykel
AW Software Works

> Hello Roshawn,
>
[quoted text clipped - 19 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Wen Yuan Wang [MSFT] - 09 May 2008 09:42 GMT
Hello Alexander,
Thanks for your reply.

Do you talk about the default name solution? I think it's fine to extent
XElement class. We can add our own namespace in subclass. Thereby, subclass
could query the element by added default namespace. That should work.
Anyway,  if you face any further issue when implementing this solution,
please also feel free to let us know. We are glad to assist you.

Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.