how can i change xml string in C# ????
- hi all ,
i hav sml string as
<?xml version="1.0" encoding="Windows-1252" ?>- <VisualStudioProject ProjectType="Visual C++" Version="9.00" Name="tryyy" ProjectGUID="{B1FDB037-0B8F-4C84-A239-77F4093FF5F5}" Keyword="CustomAppWizProj" TargetFrameworkVersion="196613" ConfigurationType="2" genre="novel"><Platform Name="Win32" /></Platforms><ToolFiles />- <Configuration Name="Debug|Win32" OutputDirectory="$(SolutionDir)$(ConfigurationName)" IntermediateDirectory="$(ConfigurationName)" ConfigurationType="10"><Tool Name="VCPreBuildEventTool" /><Tool Name="VCCustomBuildTool" /><Tool Name="VCMIDLTool" /><Tool Name="VCPostBuildEventTool" /></Configuration><Tool Name="VCPreBuildEventTool" /><Tool Name="VCCustomBuildTool" /><Tool Name="VCMIDLTool" /><Tool Name="VCPostBuildEventTool" /></Configuration></Configurations><References /><File RelativePath=".\Templates\1033\ReadMe.txt" /><File RelativePath=".\Templates\1033\Sample.txt" /></Filter><File RelativePath=".\HTML\1033\default.htm" DeploymentContent="true" /></Filter><Filter Name="Image Files" Filter="bmp" /><File RelativePath=".\Scripts\1033\default.js" /></Filter><File RelativePath=".\default.vcproj" /><File RelativePath=".\1033\NewStyles.css" /><File RelativePath=".\Templates\1033\Templates.inf" /><File RelativePath=".\tryyy.ico" /><File RelativePath=".\tryyy.vsdir" /><File RelativePath=".\tryyy.vsz" /></Filter><File RelativePath=".\1033\Images\DottedHori.gif" /><File RelativePath=".\1033\Images\DottedVert.gif" /><File RelativePath=".\1033\Images\spacer.gif" /><File RelativePath=".\Images\tryyy.gif" /><File RelativePath=".\Images\tryyy_Background.gif" /></Files><Globals /></VisualStudioProject>
i m using code to change my xml string as ...
XmlDocument
xd = new XmlDocument();
xd.LoadXml("AboveString");
XmlElement root = xd.DocumentElement;
root.SetAttribute("ConfigurationType", "2");
my problm is .. here i m able to change the ConfigurationType value under rootnode VisualStudioProject bt i wann to change the ConfigurationType value under Configuration node also ..
hw can i do it ??? can anyone help ???
thx in advnce
aniruddha
Answers
Use SelectNodes then.
XmlDocument xd = new XmlDocument();xd.Load(@"C:\dump\visualstudioproject.xml");
XmlNodeList nodes = xd.SelectNodes("descendant::Configuration");
if (nodes != null)
foreach (XmlElement el in nodes)
el.SetAttribute("ConfigurationType", "2");
Console.WriteLine(xd.OuterXml);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byaniruddha84 Wednesday, November 04, 2009 8:10 AM
All Replies
- XmlElement el = (XmlElement)root.SelectSingleNode(@"//VisualStudioProject/Configurations/Configuration");
el.SetAttribute("ConfigurationType", "3");
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - hi David
it bit look lik tat u r givng the xpath .. bt its seems some hardcode ..
isted of (@"//VisualStudioProject/Configurations/Configuration"); can i do this with program... so may b tomorow my xml file change i dont hav to change code ??
if VisualStudioProject is root node thn wat shud i call Configurations ?? and Configuration ?? i think i can add value lik tat n i ll start .. Use SelectNodes then.
XmlDocument xd = new XmlDocument();xd.Load(@"C:\dump\visualstudioproject.xml");
XmlNodeList nodes = xd.SelectNodes("descendant::Configuration");
if (nodes != null)
foreach (XmlElement el in nodes)
el.SetAttribute("ConfigurationType", "2");
Console.WriteLine(xd.OuterXml);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byaniruddha84 Wednesday, November 04, 2009 8:10 AM
- hi David ,
please check this part of xml string
Configurations>- <Configuration Name="Debug|Win32" OutputDirectory="$(SolutionDir)$(ConfigurationName)" IntermediateDirectory="$(ConfigurationName)" ConfigurationType="10"><Tool Name="VCPreBuildEventTool" /><Tool Name="VCCustomBuildTool" /><Tool Name="VCMIDLTool" /><Tool Name="VCPostBuildEventTool" /></Configuration><Tool Name="VCPreBuildEventTool" /><Tool Name="VCCustomBuildTool" /><Tool Name="VCMIDLTool" /><Tool Name="VCPostBuildEventTool" /></Configuration></Configurations>
ur code is working for first ConfigurationType for second one its not working ..
any clue ???
thx lots for ur guidance - I'm really not sure. It's working fine for me. My second code doesn't set the root node's ConfigurationType value, mainly because it's not a "Configuration" element. But it sets the ConfigurationType attribute of every Configuration element in the document.
I don't know why it's not working for you. Post the code you're using currently.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - ok fine here my code goes ....
its bit hectic first time ... bcoz i m parsing xml file n comparing each valu with my reference value contain in my disconary dic.. if values are mismatch i m chaging tat valu as per ur logic .. pls let me knw whr i m wrong .. or anyothr easy way ..
in my dic ( disconry ) contain only xreader.name and correspondin xreader.value .....
XmlDocument xd = new XmlDocument(); xd.Load(this.Projct); xd.LoadXml(xd.InnerXml); XmlElement root = xd.DocumentElement; System.IO.StringReader sReader = new System.IO.StringReader(xd.InnerXml); int index = 0; XmlReader xReader = XmlReader.Create(sReader); string result; string reval; int result1 = 0; int res = 0; string s1 = ""; string s = ""; XmlNodeList node = xd.SelectNodes("descendant::Configuration"); foreach (XmlElement el in node) { while (xReader.Read()) { switch (xReader.NodeType) { case XmlNodeType.Element: if (xReader.HasAttributes) { for (int i = 0; i < xReader.AttributeCount; i++) { xReader.MoveToAttribute(i); if (xReader.Name != "Name") { if (xReader.Name != "RelativePath") { if (xReader.Name != "Filter") { foreach (List<string> val in dic.Keys) { result = val.Find(item => item == xReader.Name.ToString()); index = val.IndexOf(xReader.Name.ToString()); try { s = val[index]; } catch (ArgumentOutOfRangeException) { } result1 = string.Compare(s, xReader.Name); } foreach (List<string> val1 in dic.Values) { reval = val1.Find(item => item == xReader.Value.ToString()); try { s1 = val1[index]; } catch (ArgumentOutOfRangeException) { } res = string.Compare(s1, reval); if (result1 == 0 && res == 0) { } if (result1 == 0 && res != 0) { el.SetAttribute(xReader.Name, s1); } } } } } } } break; default: break; } } } xd.Save(this.Projct); There's so many nested loops and if statements here I can't really tell what's going on.
Is the only task you're trying to accomplish changing the ConfigurationType attribute? If so, delete all these nested loops and just go with what I've demonstrated in my second post.
Also, if you have this many conditions you need to test for, consider using the XDocument class and LINQ instead of XmlDocument. It'll clean up your code greatly.
This is a far cry from the 4 lines I showed above that allowed you to change the ConfigurationType element on every Configuration element in the whole document.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- thx David,
ur logic minmise my code .. thx.. its reli helpful


