
Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Thanks a lot.
Now when I have a look on what I wrote down, I am little bit confused,
that I forgot to write down closing tags. Below I present corrected
input and outputfile. There is some additional elements which I also
forgot to write down (no idea what I was thinking about during posting
previous message).
input
<root>
<Module>
<Def></Def>
<Elements>
<Element att1="1" att2="2" att3="3"/> // att3 is optional
.......
</Elements>
</Module>
//next modules
</root>
file I want to generate
<root>
<Module>
<Id>
<att1></att1>
</Id>
<Def>
<att2></att2>
</Def>
<Spec>
<att3></att3> //in input file att3 is optional, same here
<Spec> and att3 are optional
</Spec>
</Module>
//next modules
</root>
The XSLT looks nice, but I need to do it with C#, because I will do
some additional transformation on these attributes. So far I have
little problem with placing them in appropraite elements. I am using
.NET 2.0
> > <root>
> > <Def></Def>
[quoted text clipped - 105 lines]
> documentation does not help then ask again but tell us which .NET
> version you are using.
Thanks a lot.
Now when I have a look on what I wrote down, I am little bit confused,
that I forgot to write down closing tags. Below I present corrected
input and outputfile. There is some additional elements which I also
forgot to write down (no idea what I was thinking about during posting
previous message).
input
<root>
<Module>
<Def></Def>
<Elements>
<Element att1="1" att2="2" att3="3"/> // att3 is optional
.......
</Elements>
</Module>
//next modules
</root>
file I want to generate
<root>
<Module>
<Id>
<att1></att1>
</Id>
<Def>
<att2></att2>
</Def>
<Spec>
<att3></att3> //in input file att3 is optional, same here
<Spec> and att3 are optional
</Spec>
</Module>
//next modules
</root>
The XSLT looks nice, but I need to do it with C#, because I will do
some additional transformation on these attributes. So far I have
little problem with placing them in appropraite elements. I am using
.NET 2.0
> > <root>
> > <Def></Def>
[quoted text clipped - 105 lines]
> documentation does not help then ask again but tell us which .NET
> version you are using.
Jakub.Bednarczuk@gmail.com - 25 Oct 2006 09:42 GMT
I forgot to write what solution do I have so far
XmlDocument doc= new XmlDocument();
doc.Load("filename");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//Elements/Element/@*");
foreach (XmlNode node in nodes)
{
string attr = node.Value;
method(attr);
WriteEndElement();
}
method(strong attr){
WriteStartElement("Uid");
WriteString(attr);
WriteEndElement();
}
methods has more attributes but here is not the case tpo write all of
them. Simply I would like to make some conditions where to place value
of each attribute. I mean that
XmlNodeList nodes = root.SelectNodes("//Elements/Element/@*"); takes
values of attributes but it doesnt store names of these attributes.
Simply I have no idea how to put attributes from one node to different
elements
Jakub.Bednarc...@gmail.com wrote:
> Thanks a lot.
> Now when I have a look on what I wrote down, I am little bit confused,
[quoted text clipped - 151 lines]
> > Martin Honnen --- MVP XML
> > http://JavaScript.FAQTs.com/
Martin Honnen - 25 Oct 2006 18:25 GMT
> input
> <root>
[quoted text clipped - 27 lines]
> The XSLT looks nice, but I need to do it with C#, because I will do
> some additional transformation on these attributes.
Here is C# DOM code
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
foreach (XmlElement module in
xmlDocument.SelectNodes(@"/root/Module")) {
foreach (XmlElement element in
module.SelectNodes(@"Elements/Element")) {
element.ParentNode.RemoveChild(element);
XmlElement id = xmlDocument.CreateElement("Id");
XmlElement att1 = xmlDocument.CreateElement("att1");
att1.InnerText = element.GetAttribute("att1");
id.AppendChild(att1);
module.InsertBefore(id, module.FirstChild);
XmlElement att2 = xmlDocument.CreateElement("att2");
att2.InnerText = element.GetAttribute("att2");
module["Def"].AppendChild(att2);
if (element.HasAttribute("att3")) {
XmlElement spec = xmlDocument.CreateElement("Spec");
XmlElement att3 = xmlDocument.CreateElement("att3");
att3.InnerText = element.GetAttribute("att3");
spec.AppendChild(att3);
module.InsertAfter(spec, module["Def"]);
}
}
module.RemoveChild(module["Elements"]);
}
xmlDocument.Save(Console.Out); // save to file here in real app
Console.WriteLine();
The input
<root>
<Module>
<Def></Def>
<Elements>
<Element att1="1" att2="2" att3="3"/>
</Elements>
</Module>
</root>
ends up as
<root>
<Module>
<Id>
<att1>1</att1>
</Id>
<Def>
<att2>2</att2>
</Def>
<Spec>
<att3>3</att3>
</Spec>
</Module>
</root>
That might or might not do what you want if there are several |Element|
elements but your description is still not clear to me. Nevertheless the
example should how how to create element nodes, how to remove nodes and
how to insert newly created nodes.

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jakub.Bednarczuk@gmail.com - 26 Oct 2006 08:57 GMT
Thanks a lot. I solved this problem yesterday in the afternoon, but
nevertheless thanks for help
> > input
> > <root>
[quoted text clipped - 93 lines]
> example should how how to create element nodes, how to remove nodes and
> how to insert newly created nodes.