[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Editing an XML file through an ASP.NET Web Application

Posted on 2009-02-18
9
Medium Priority
?
263 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:lvneal
  • 5
  • 4
9 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 23675754
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

0
 
LVL 27

Expert Comment

by:nmarun
ID: 23675759
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

0
 

Author Comment

by:lvneal
ID: 23676115
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,
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 27

Expert Comment

by:nmarun
ID: 23676146
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.
0
 

Author Comment

by:lvneal
ID: 23681641
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,

0
 
LVL 27

Expert Comment

by:nmarun
ID: 23682664
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.
0
 

Author Comment

by:lvneal
ID: 23687068
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
0
 
LVL 27

Accepted Solution

by:
nmarun earned 1500 total points
ID: 23687176
0
 

Author Closing Comment

by:lvneal
ID: 31548516
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question