Link to home
Start Free TrialLog in
Avatar of lvneal
lvneal

asked on

Editing an XML file through an ASP.NET Web Application

Hi,

I'm relatively new to ASP.NET and XML. I've been using the tutorial on http://aspnet.4guysfromrolla.com/articles/112603-1.aspx which works (I downloaded the source code from http://aspnet.4guysfromrolla.com/articles/112603-1.2.aspx) - but the XML I need to adapt this code to is slightly different as shown below. I.e. I have a tag within a tag. I assume I have to change a data container item? If someone could provide some exmaple code (or could show me the modifications required in the code) it would be greatly appreciated.

Thanks,
<?xml version="1.0" standalone="yes"?>
<root>
  <record>
    <aid>1</aid>
    <name>Tahir</name>
    <dept><newtag>IT</newtag></dept>
    <salary>4000</salary>
  </record>
</root>

Open in new window

Avatar of nmarun
nmarun
Flag of India image

I'm giving an example from the link you provided.



//Change your ItemTemplate in the data grid to something like this:
 
<asp:DataGrid id="DataGrid1" runat="server" ...>
  <Columns>
    <asp:TemplateColumn HeaderText="Id">
       <ItemTemplate>
         <%# GetDept(DataBinder.Eval(Container.DataItem,"dept")) %>
       </ItemTemplate>
    </asp:TemplateColumn>
    ...
  </Columns>
</asp:DataGrid>
 
//code-behind
 
Function ShowVal(dept as string)
   Dim actualDept as string   
   actualDept = dept.Replace("<newtag>", "").Replace("</newtag>", "")
   Return actualDept
End Function

Open in new window

sorry.. try this:

//Change your ItemTemplate in the data grid to something like this:
 
<asp:DataGrid id="DataGrid1" runat="server" ...>
  <Columns>
    <asp:TemplateColumn HeaderText="Dept">
       <ItemTemplate>
         <%# GetDept(DataBinder.Eval(Container.DataItem,"dept")) %>
       </ItemTemplate>
    </asp:TemplateColumn>
    ...
  </Columns>
</asp:DataGrid>
 
//code-behind
 
Function ShowVal(dept as string)
   Dim actualDept as string   
   actualDept = dept.Replace("<newtag>", "").Replace("</newtag>", "")
   Return actualDept
End Function

Open in new window

Avatar of lvneal
lvneal

ASKER

Thanks nmarun,

Is the function meant to be called "GetDept"?

So, if a tag value is nested within another tag (not sure if that's the correct terminology), you would just replace the otter value with the inner value?

The actual code gets more complex:

<?xml version="1.0" standalone="yes"?>
<root>
  <record>
    <aid>1</aid>
    <name>Tahir</name>
    <dept><newtag><tag2 value1="", value2="", value3="IT"></newtag><newtag2><tagAB valueA1="", valueB1=""></newtag2></dept>
    <salary>4000</salary>
  </record>
</root>

I'm trying to figure out how to read the values in value1, value2 and value3, then values valueA1, valueB1 etc. (I know this isn't the best XML file formatting - I was given the xml file and I'm not allowed to change it!).

Any help would be great.

Thanks,
So how exactly does your <dept> code looks like? I did a simple string replacement because it was a simple string. If it is complicated, we'll have to look into it.

Also let me know how you want that information to be displayed in the datagrid.
Avatar of lvneal

ASKER

The actual xml code looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PerformanceData>
 <Machine aid="1" x2="0.71" x1="0.08" Constant="22.69" MW="13.1" Weight="250" Length="45" Width="19" Height="120" Notes="notes"       NA="0"  NB="0.61"  NC="0" NNotes="notes" DNA="0" DNB="0" DNC="0.24"  ct_Eff_Ld="3.23" x_Eff_Ld="0.6" x2_Eff_Ld="-0.1" P100="46.4" PMxRating="2.9" Category="1"/>

 <Machine aid="2" x2="-0.2" x1="0.2" Constant="10.16" MW="12.1" Weight=""  NA="0"  NB="0.01"  NC="0" NNotes="notes" DNA="0" DNB="0" DNC="0.4">
    <Efficiency>
      <PLdVsEff MinLd="0" MaxLd="33" x2="0.1" x1="0.5" Constant="10.64"  Category="1"/>
      <PLdVsEff MinLd="33" MaxLd="63" x2="-0.01" x1="0.6" Constant="9.4"  Category="1"/>
      <PLdVsEff MinLd="63" MaxLd="100" x2="-0.1" x1="1.5" Constant="-28.1"  Category="1"/>
    </Efficiency>
    <NOx>
      <NkghrvsNTIMW MinMW="0" MaxMW="34.5" x2="-0.09" x1="1.7" Constant="-20.18" Category="1"/>
      <NkghrvsNTIMW MinMW="34.5" MaxMW="53" x2="0.2 x1="-0.48" Constant="18.97" Category="1"/>
      <NkghrvsNTIMW MinMW="53" MaxMW="100" x2="0.02" x1="-2.75" Constant="93.84" Category="1"/>
    </NOx>
 </Machine>
</PerformanceData>

That's an example of two different machines which have different data in each. I need to be able to edit this xml file through a "nice" interface via a web application. For the first machine it is fine for a table and I can see how it would work...for the second though - there are extra elements.

I think the best way to visualise the information on screen would be to make a dropdown list with the names of each machine - then when one is selected, text boxes appear below with the data for that specific machine - rather than displaying everything in one big table.

How would you reference a specific element in XML from the ASP page?

Thanks,

Firstly, it doesn't look like a standard that can be shared between machines. <Machine> tags have things that are not common between them. See if you can standardize these tags, if not you'll have a tough time to check for every attribute/child node for every machine tag and then dynamically add a control on to the web page.

If you have a standardized xml, you can pre-define the controls and then show the values accordingly.
Avatar of lvneal

ASKER

Unfortunately I have to leave the XML file as it is. Rather than binding to a datagrid, I plan to display items in textboxes individually. I am using:

txtX2.Text = objdata.Tables("GasTurbine").Rows(i).Item("x2")

to obtain the data in the x2 element. My question now is, how do I reference the MaxLd data which is inside the <Efficiency> and <PLdVsEff> elements?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of nmarun
nmarun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lvneal

ASKER

Thanks