Niranjan_007

Hi pals i am a newbie in software.I am loading the contents of an XML File into the Screen using C#.When they save the contents i am a creating a new XML File.If they load the newly created XML and again make some changes i have to replace the old contents of the XML and save it.I am able to replace the InnerText of all the nodes but i am not able to replce the attributes of the Nodes.Currently i am using the following approach:

XmlDocument LetterDoc = new XmlDocument();

LetterDoc.Load(__strPhyLetterFileName);

XmlNodeList PageNodes = LetterDoc.SelectNodes(TEMPLATE + "/" + TEMPLATE_TYPE + "/" + TEMPLATE_ID);

XmlNode xlnode = LetterDoc.SelectSingleNode(TEMPLATE_ID);

foreach (XmlNode node in PageNodes)

{

XmlAttributeCollection xmlAttributeCollection = node.Attributes;

foreach (XmlAttribute x in xmlAttributeCollection)

{

if (x.Value.ToString().Trim() == strTemplatename)

{

XmlNode xSubNode = node.SelectSingleNode(SUBJECT);

xSubNode.InnerText = txtSubject.Text.ToString().Trim();

XmlNode xSalutationNode = node.SelectSingleNode(SALUTATION);

xSalutationNode.InnerText = txtSalutation.Text.ToString().Trim();

XmlNode xBodyNode = node.SelectSingleNode(BODY);

//xBodyNode.InnerText = txtBody.Text.ToString().Trim();

xBodyNode.InnerText = body.ToString().Trim();

XmlNode xCloseNode = node.SelectSingleNode(CLOSING);

xCloseNode.InnerText = txtClosing.Text.ToString().Trim();

}

}

LetterDoc.Save(__strPhyLetterFileName);

}

Is there any method to save the attributes



Re: XML and the .NET Framework How to replcae the attributes of an XML File

Martin Honnen

If you want to change the value of an attribute then simply do e.g.

Code Snippet
x.Value = "new value"

If you want to remove an attribute then use e.g.

Code Snippet
node.RemoveAttribute("attribute-name")






Re: XML and the .NET Framework How to replcae the attributes of an XML File

Niranjan_007

Thanks but wat if i have 2 attributes to the same node.How will i specify the attributes name so that ill replace the required value.



Re: XML and the .NET Framework How to replcae the attributes of an XML File

Martin Honnen

If you have an element node (XmlElement instance) then you can use the SetAttribute method e.g.

Code Snippet
elementNode.SetAttribute("attribute-name", "new value")

or you can index the Attributes collection and set the Value property e.g.

Code Snippet

elementNode.Attributes["attribute-name"].Value = "new value"






Re: XML and the .NET Framework How to replcae the attributes of an XML File

Niranjan_007

Hi Thanks but if i use XMLElement instance then how i think i have to create new elements.Will i be able to replace the existing node's values with tat.Pls gimme an eg.A piece of code explaining this would be more than appreciated.



Re: XML and the .NET Framework How to replcae the attributes of an XML File

Martin Honnen

Can you show us an XML sample that you want to manipulate and show us the resulting XML you want to create




Re: XML and the .NET Framework How to replcae the attributes of an XML File

Niranjan_007

Here is the sample XML

<a>

<b enabled="Y" sequence="1">InnerText1</b>

<c enabled="N" sequence="3">InnerText2</c>

<b enabled="Y" sequence="2">InnerText3</b>

</a>

Now i need to fetch this XML and change all the attributes and InnerTexts.But i am facing trouble in replacing the attributes as there are 2 attributes.My new XML shoulb look like this:

<a>

<b enabled="N" sequence="3">InnerText4</b>

<c enabled="N" sequence="2">InnerText5</c>

<b enabled="Y" sequence="1">InnerText6</b>

</a>

Rite now i am using the method posted in the initial post of this thread.Is tat method ok or shud i have to switch to some other method.An explanation with some eg., code would be more than handy.





Re: XML and the .NET Framework How to replcae the attributes of an XML File

Martin Honnen

Here is a C# sample doing all the changes:

Code Snippet

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
XmlElement b = xmlDocument.DocumentElement["b"] as XmlElement;
if (b != null) {
b.InnerText = "InnerText4";
b.SetAttribute("enabled", "N");
b.SetAttribute("sequence", "3");
}
XmlElement c = xmlDocument.DocumentElement["c"] as XmlElement;
if (c != null) {
c.InnerText = "InnerText5";
c.SetAttribute("sequence", "2");
}
b = xmlDocument.DocumentElement.SelectSingleNode("b[2]") as XmlElement;
if (b != null) {
b.InnerText = "InnerText6";
b.SetAttribute("sequence", "1");
}
xmlDocument.Save("file.xml");