Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

XML namespace and LINQ to XML

116 views
Skip to first unread message

Alexander Wykel

unread,
Apr 9, 2008, 6:00:00 PM4/9/08
to
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

--
Alexander L. Wykel
AW Software Works

Wen Yuan Wang [MSFT]

unread,
Apr 9, 2008, 11:19:42 PM4/9/08
to
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:
msd...@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

unread,
Apr 15, 2008, 3:59:00 AM4/15/08
to
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


--
Alexander L. Wykel
AW Software Works

Wen Yuan Wang [MSFT]

unread,
Apr 16, 2008, 12:44:34 AM4/16/08
to
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:
msd...@microsoft.com.
==================================================

Roshawn

unread,
Apr 17, 2008, 9:20:21 PM4/17/08
to
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]

unread,
Apr 18, 2008, 1:31:37 AM4/18/08
to
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.

Alexander Wykel

unread,
May 8, 2008, 12:43:02 PM5/8/08
to
Do you see any design problems with this, I wil impliment this if you think
it will work.

--
Alexander L. Wykel
AW Software Works

Wen Yuan Wang [MSFT]

unread,
May 9, 2008, 4:42:16 AM5/9/08
to
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.

0 new messages