Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with code to read XML file and update XML file

Status
Not open for further replies.

fr2

Programmer
May 13, 2008
45
US
I need help reading and modifying this XMLfile ... I need to modify the resource element and convert it to UPPER case i.e.

<file href="scos/SCOFunctions.js"/>
.. to ...
.... <file href="scos/scoFunctions.js"/>


I tried reading it into a dataset but all I got was this

manifest_Id identifier version
0 SingleLocalSCO 1.2

I guess because of the hierachchical nature, I am missing the <organizations> and <resources> related table elements ..

Also I am not sure if I am including the schema for the XMLFile in reading it ...

I am not sure how to reference the schema when the schema is in a different file (say sch_rootv1p1p2.xsd )...

this is how I read it into the dataset

dsMyAuthors.ReadXml(filePath, Data.XmlReadMode.ReadSchema) ...

Any help on how to go about it pelase!









<?xml version="1.0" standalone="no"?>
<manifest identifier="SingleLocalSCO" version="1.2" xmlns:adlcp=" xmlns=" xmlns:xsi=" xmlns:imsmd=" xsi:schemaLocation=" imscp_rootv1p1p2.xsd imsmd_rootv1p2p1.xsd adlcp_rootv1p2.xsd">
<organizations default="TOC1">
<organization identifier="TOC1">
<title>Smart Services</title>
<item identifier="ITEM1" identifierref="RESOURCE1">
<title>Smart Services Video</title>
</item>
</organization>
</organizations>
<resources>
<resource identifier="RESOURCE1" adlcp:scormtype="sco" type="webcontent" href="scos/v_launch.html?fn=503sv&amp;puwc=off">
<file href="scos/v_launch.html"/>
<file href="scos/503sv_av20.flv"/>
<file href="scos/503sv_av225.flv"/>
<file href="scos/503sv.txt"/>
<file href="scos/fmd.swf"/>
<file href="scos/SCOFunctions.js"/>
<file href="scos/cyberimage/f503v.jpg"/>
</resource>
</resources>
</manifest>
 
load the xml document using a xmldocument object.

navigate to the node using xpath and associated object members, change the value. overwrite the existing file.
Code:
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.FindNode("//resource");
//change node as necessary
doc.Save(filename);
there are plenty of details online. the objects are found in System.Xml namespace

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Cant find "FindNode" method on the XMLDocument object

... doc.FindNode ?

Also for the resource node there are several file children

<resource identifier="RESOURCE1" adlcp:scormtype="sco" type="webcontent" href="scos/v_launch.html?fn=503sv&amp;puwc=off">
<file href="scos/v_launch.html"/>
<file href="scos/503sv_av20.flv"/>
<file href="scos/503sv_av225.flv"/>
<file href="scos/503sv.txt"/>
<file href="scos/fmd.swf"/>
<file href="scos/SCOFunctions.js"/>
<file href="scos/cyberimage/f503v.jpg"/>
</resource>

can you give a hint on actual code to read each
<file href="scos/SCOFunctions.js"/> element and test and convert to lower case .... how do I then replace a particular element in the loaded file

... before finally saving the entire file ..

Pls
 
try selectnode. the msdn help has all this documented.
xpath is a standard xml query syntax, there is plenty of resources online.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
In c#, the algorithm goes like this.
[tt]
XmlDocument oparser=new XmlDocument();
oparser.Load(in_xml);
XmlNamespaceManager nsmgr=new XmlNamespaceManager(oparser.NameTable);
nsmgr.AddNamespace("dns","[ignore][/ignore]");
XmlNodeList onodelist=oparser.SelectNodes("/dns:manifest/dns:resources/dns:resource/dns:file/@href[.='scos/SCOFunctions.js']",nsmgr);
foreach (XmlNode onode in onodelist) {
onode.Value="scos/scoFunctions.js";
}
oparser.Save(out_xml);
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top