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
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 L. Wykel
--
Alexander L. Wykel
AW Software Works
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.
==================================================
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 :)
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.
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.