Solved

Return style elements to New Xattribute

Posted on 2011-09-26
15
358 Views
Last Modified: 2013-11-11
Would like to know how to return a style element to a New Xattribute.  The following code doesn't work, but I think you can understand what I'm trying to accomplish.  It returns the "Y" or "N", but not the color.
               New XElement("td",
                                New XAttribute("align", "center"), _
                                New XAttribute("class", "standard"), _
                                New XAttribute("style", "width: 25px"), _
                                IsReprint(_item.IsReprint)), _


Public Function IsReprint(ByVal Reprint As String) As String

    If (Reprint = "Y") Then

        Return New XElement("td", _
               New XAttribute("style", "color : red"), String.Format("{0}", "Y"))
    Else
        Return New XElement("td", _
               New XAttribute("style", "color : black"), String.Format("{0}", "N"))

    End If

End Function

Open in new window

0
Comment
Question by:JRFromSoCal
  • 8
  • 5
15 Comments
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36709834
Thank you for moving into correct zone.  However, I'm not sure what moved code into snippet with some formatting means.

J.R.
0
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36709951
Thanks aikimark.  Next time I post code I'll be sure to hit the code button.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36712496
You are using String.Format to change the value to Y and N.

If (Reprint = "Y") Then

        Return New XElement("td", _
               New XAttribute("style", "color : red"), String.Format("{0}", "Y"))
    Else
        Return New XElement("td", _
               New XAttribute("style", "color : black"), String.Format("{0}", "N"))

    End If

Open in new window

0
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36712550
Hi TheLearnedOne:

Thanks for responding.  I'm getting the Y and N returned with no problem.  What my goal is: is to have the Y returned in the color red and the N returned in the color black.  That piece isn't happening.  For some reason the style elements aren't being returned.  I'm not sure if I've got the logic coded correctly.  If I code the style attributes when I'm declaring the xattributes (above) it works fine.  However, I can't put any if logic in it.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36712649
Can you show me a snippet of the XML that you expect, so that I can understand?
0
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36713176
Hi TheLearnedOne:

Below is the snippet you requested.  This data fills a grid.  If you notice the line with the align center.  I need that Y to be red.  The code in my original posting produces this.  I'm not sure how to get the Y to display red after the grid has been loaded.

J.R.



<tr id="20192" subid="2" impressioncontrolid="0" jobid="47" partcount="1" reprintid="645" statusid="1">
    <td align="left" class="standard1" style="width: 100px">20192</td>
    <td align="left" class="standard" style="width: 25px">2</td>
    <td align="left" class="standard" style="width: 25px">KMP</td>
    <td align="center" class="standard" style="width: 25px">Y</td>
    <td align="left" class="standard" style="width: 100px">20# WHITE</td>
    <td align="left" class="standard" style="width: 100px">BLUE CHECKS</td>
    <td align="left" class="standard" style="width: 100px"></td>
    <td align="left" class="standard" style="width: 100px"></td>
    <td align="left" class="standard" style="width: 25px">0</td>
    <td align="left" class="standard" style="width: 25px">0</td>
    <td align="left" class="standard" style="width: 125px">09-24-2008</td>
    <td align="left" class="standard" style="width: 25px"></td>
  </tr>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 36713199
Do you need the element to be like this?

<td align="center" class="standard" style="width: 25px;color: red">Y</td>

Open in new window

0
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36713252
I could produce that red Y by making the following adjustment to the xattribute declaration.

New XAttribute("style", "color:red; width: 25px;"), IsReprint(_item.IsReprint)), _

However, I need the Y's to be red and the N's to be black.  If I were to code it like the above, both results would be red.

J.R.
New XAttribute("style", "color:red; width: 25px;"), IsReprint(_item.IsReprint)), _

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36713293
I would think something like this might work:


Dim td As New XElement("td")

Dim style As New StringBuilder("width: 25px;")

If (Reprint = "Y") Then
   style.Append("color: red;")   
Else
   style.Append("color: black;")
End If

td.Add(New XAttribute("style", style, Reprint))

Return td

Open in new window

0
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36713334
This looks great.  However, I'm getting the following error:  Overload resolution failed because no accessible ‘New” accepts this number of arguments.

J.R.
td.Add(New XAttribute("style", style, Reprint))

Open in new window

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36717157
That's what you get when you code in the comment block (d'oh).




Public Function CreateTableCell() As XElement
        Dim td As New XElement("td")

        Dim style As New StringBuilder("width: 25px;")

        If (Reprint = "Y") Then
            style.Append("color: red;")
        Else
            style.Append("color: black;")
        End If

        td.Add(New XAttribute("style", style), Reprint)

        Return td
    End Function

Open in new window

0
 
LVL 1

Author Comment

by:JRFromSoCal
ID: 36717526
Works like a charm.  I tried the return type as a string and a xattribute.  For some stupid reason I never thought to try the return type as an xelement.  You're awesome TheLearnedOne.  Thanks so much for your help.

J.R.
0
 
LVL 1

Author Closing Comment

by:JRFromSoCal
ID: 36717539
Works like charm.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now