 |
| C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 23rd, 2011, 05:36 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
General xml creation issue
Hi All
I am just writing a method to generate a xml document from scratch. I am using .NET Framework 4.0 and VS 2010.
Here is my method to generate the xml:
Code:
private void CreateMyXML()
{
// creating xml document
XmlDocument xmldoc = new XmlDocument();
// creating root element
XmlElement root = xmldoc.CreateElement("Modularity:ModuleCatalog");
// Now add attributes to the root element
root.SetAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
root.SetAttribute("xmlns:x", "http://schemas.microsoft.com/winfx/2006/xaml");
root.SetAttribute("xmlns:sys", "clr-namespace:System;assembly=mscorlib");
root.SetAttribute("xmlns:modularity", "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism");
xmldoc.AppendChild(root);
XmlElement elem1 = xmldoc.CreateElement("Modularity:ModuleInfo");
// Now add attributes to this element
elem1.SetAttribute("Ref", "Infor.PLM.Modules.WorkspaceBarModule.xap");
elem1.SetAttribute("ModuleName", "WorkspaceBarModule");
elem1.SetAttribute("ModuleType", "Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null");
root.AppendChild(elem1);
elem1 = xmldoc.CreateElement("Modularity:ModuleInfo");
// Now add attributes to this element
elem1.SetAttribute("Ref", "Infor.PLM.Modules.StencilBarModule.xap");
elem1.SetAttribute("InitializationMode", "OnDemand");
elem1.SetAttribute("ModuleName", "StencilBarModule");
elem1.SetAttribute("ModuleType", "Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null");
root.AppendChild(elem1);
string xmlOutput = xmldoc.OuterXml;
}
The output which i get is not correct because the root element & its child element "local name" appears but i want the "name" instead. What needs to be modified in this? What code I am missing?
-- Abhishek
|
|

July 23rd, 2011, 06:04 AM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
Hello Abhishek,
What's your issue? This is the XML I see generated:
Code:
<ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<ModuleInfo Ref="Infor.PLM.Modules.WorkspaceBarModule.xap" ModuleName="WorkspaceBarModule" ModuleType="Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
<ModuleInfo Ref="Infor.PLM.Modules.StencilBarModule.xap" InitializationMode="OnDemand" ModuleName="StencilBarModule" ModuleType="Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null" />
</ModuleCatalog>
And why don't you use the newer XElement and XAtribute classes? It's so much easier to create XML with it:
Code:
XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XElement xml = new XElement(ns + "ModuleCatalog",
new XAttribute("xmlns", ns),
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute(XNamespace.Xmlns + "sys", "clr-namespace:System;assembly=mscorlib"),
new XAttribute(XNamespace.Xmlns + "modularity", "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism"),
new XElement(ns + "ModuleInfo",
new XAttribute("Ref", "Infor.PLM.Modules.WorkspaceBarModule.xap"),
new XAttribute("ModuleName", "WorkspaceBarModule"),
new XAttribute("ModuleType", "Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null")
),
new XElement(ns + "ModuleInfo",
new XAttribute("Ref", "Infor.PLM.Modules.StencilBarModule.xap"),
new XAttribute("InitializationMode", "OnDemand"),
new XAttribute("ModuleName", "StencilBarModule"),
new XAttribute("ModuleType", "Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null")
)
With the Visual Studio samples you'll find an add-in "Paste XML as XElement" that creates this code out of XML.
Hope this helps.
|
|

July 23rd, 2011, 06:13 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Hi Christian
If you see that the root element which I declared is "Modularity:ModuleCatalog" but when xml is generated, only "ModuleCatalog" comes. I debugged and checked and what I found is that ModuleCatalog is the local name and Modularity:ModuleCatalog is the name. Localname and name are both properties. I want name property to appear, to be precise I want Modularity:ModuleCatalog to appear instead of ModuleCatalog in the generated xml.
I hope you got my point. The output should be like this:
Code:
<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<Modularity:ModuleInfo Ref="Infor.PLM.Modules.WorkspaceBarModule.xap"
ModuleName="WorkspaceBarModule"
ModuleType="Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
<Modularity:ModuleInfo Ref="Infor.PLM.Modules.StencilBarModule.xap"
InitializationMode="OnDemand"
ModuleName="StencilBarModule"
ModuleType="Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null" />
</Modularity:ModuleCatalog>
Thanks
Abhishek
|
|

July 23rd, 2011, 06:24 AM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
ok, I see the point. Use this overload of CreateElement to create the root element:
Code:
XmlElement root = xmldoc.CreateElement("Modularity", "ModuleCatalog", "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism");
As an alternative with XElement:
Code:
XNamespace Modularity = "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism";
XElement xml = new XElement(Modularity + "ModuleCatalog",
new XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"),
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute(XNamespace.Xmlns + "sys", "clr-namespace:System;assembly=mscorlib"),
new XAttribute(XNamespace.Xmlns + "Modularity", Modularity),
new XElement(Modularity + "ModuleInfo",
new XAttribute("Ref", "Infor.PLM.Modules.WorkspaceBarModule.xap"),
new XAttribute("ModuleName", "WorkspaceBarModule"),
new XAttribute("ModuleType", "Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null")
),
new XElement(Modularity + "ModuleInfo",
new XAttribute("Ref", "Infor.PLM.Modules.StencilBarModule.xap"),
new XAttribute("InitializationMode", "OnDemand"),
new XAttribute("ModuleName", "StencilBarModule"),
new XAttribute("ModuleType", "Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null")
)
);
|
|
The Following User Says Thank You to ChristianNagel For This Useful Post:
|
|
|

July 23rd, 2011, 07:25 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Hi
I did what you told me but it didn't solved the issue but it has added some more. Here is my modified code.
Code:
private void CreateMyXML()
{
// creating xml document
XmlDocument xmldoc = new XmlDocument();
// creating root element
XmlElement root = xmldoc.CreateElement("Modularity", "ModuleCatalog", "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism");
// Now add attributes to the root element
root.SetAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
root.SetAttribute("xmlns:x", "http://schemas.microsoft.com/winfx/2006/xaml");
root.SetAttribute("xmlns:sys", "clr-namespace:System;assembly=mscorlib");
//root.SetAttribute("xmlns:modularity", "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism");
xmldoc.AppendChild(root);
XmlElement elem1 = xmldoc.CreateElement("Modularity:ModuleInfo");
// Now add attributes to this element
elem1.SetAttribute("Ref", "Infor.PLM.Modules.WorkspaceBarModule.xap");
elem1.SetAttribute("ModuleName", "WorkspaceBarModule");
elem1.SetAttribute("ModuleType", "Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null");
root.AppendChild(elem1);
elem1 = xmldoc.CreateElement("Modularity:ModuleInfo");
// Now add attributes to this element
elem1.SetAttribute("Ref", "Infor.PLM.Modules.StencilBarModule.xap");
elem1.SetAttribute("InitializationMode", "OnDemand");
elem1.SetAttribute("ModuleName", "StencilBarModule");
elem1.SetAttribute("ModuleType", "Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null");
root.AppendChild(elem1);
string xmlOutput = xmldoc.OuterXml;
}
Now the output which comes out is this:
Code:
<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism"><ModuleInfo Ref="Infor.PLM.Modules.WorkspaceBarModule.xap" ModuleName="WorkspaceBarModule" ModuleType="Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" xmlns="" /><ModuleInfo Ref="Infor.PLM.Modules.StencilBarModule.xap" InitializationMode="OnDemand" ModuleName="StencilBarModule" ModuleType="Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null" xmlns="" /></Modularity:ModuleCatalog>
Now what has happened is in both elements of my xml, it has added extra attribute "xmlns=""" , which shouldn't be there. Secondly my child element name is "ModuleInfo", which actually should be "Modularity:ModuleInfo"
The output has to be exactly like this:
Code:
<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<Modularity:ModuleInfo Ref="Infor.PLM.Modules.WorkspaceBarModule.xap"
ModuleName="WorkspaceBarModule"
ModuleType="Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
<Modularity:ModuleInfo Ref="Infor.PLM.Modules.StencilBarModule.xap"
InitializationMode="OnDemand"
ModuleName="StencilBarModule"
ModuleType="Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null" />
</Modularity:ModuleCatalog>
The other thing which I dont understand is why the compiler is not taking the 'NAME' attribute , instead of taking local name?? Seeing the other way around, it needs to take the full string value.
-- Abhishek
|
|

July 23rd, 2011, 08:13 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Hey Christian
I forgot to mention that using the XElement & XAttribute classes, the output comes out perfectly. The code which you wrote works like charm. :) But I am very keen to know how to fix this thing the way I am trying to do, that is not using these new classes.
Thanks a ton
Abhishek
|
|

July 23rd, 2011, 08:56 AM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
Hi Abhishek,
The child elements must be created with the prefix and namespace as well. I think this is it:
Code:
// creating xml document
XmlDocument xmldoc = new XmlDocument();
string modNamespace = "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism";
// creating root element
XmlElement root = xmldoc.CreateElement("Modularity", "ModuleCatalog", modNamespace);
// Now add attributes to the root element
root.SetAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
root.SetAttribute("xmlns:x", "http://schemas.microsoft.com/winfx/2006/xaml");
root.SetAttribute("xmlns:sys", "clr-namespace:System;assembly=mscorlib");
//root.SetAttribute("xmlns:modularity", "clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism");
xmldoc.AppendChild(root);
XmlElement elem1 = xmldoc.CreateElement("Modularity", "ModuleInfo", modNamespace);
// Now add attributes to this element
elem1.SetAttribute("Ref", "Infor.PLM.Modules.WorkspaceBarModule.xap");
elem1.SetAttribute("ModuleName", "WorkspaceBarModule");
elem1.SetAttribute("ModuleType", "Infor.PLM.Modules.WorkspaceBarModule.WorkspaceBarModule, Infor.PLM.Modules.WorkspaceBarModule, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null");
root.AppendChild(elem1);
elem1 = xmldoc.CreateElement("Modularity", "ModuleInfo", modNamespace);
// Now add attributes to this element
elem1.SetAttribute("Ref", "Infor.PLM.Modules.StencilBarModule.xap");
elem1.SetAttribute("InitializationMode", "OnDemand");
elem1.SetAttribute("ModuleName", "StencilBarModule");
elem1.SetAttribute("ModuleType", "Infor.PLM.Modules.StencilBarModule.StencilBarModule, Infor.PLM.Modules.StencilBarModule, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null");
root.AppendChild(elem1);
string xmlOutput = xmldoc.OuterXml;
|
|
The Following User Says Thank You to ChristianNagel For This Useful Post:
|
|
|

July 23rd, 2011, 12:11 PM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks a lot buddy! :) This trick worked!
|
|
 |