C#,vb.net,MVC,Jquery,javascript,jscript,vbscript,html,vb,sharepoint,COM,WPF,WCF,Wwf,Asp,Asp.net,questions & answers,

Latest in Sports

Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Wednesday, December 31, 2014

Cross browser compatibility - Well-formed HTML in IE8< vs. IE 9.
Applies To:
This issue applies to all the browsers that support HTML 5 specification.
Problem: 
Browsers typically detect the well-formed characteristics of XML. But when it comes to html, lots of pre-html5 based browsers used to ignore the tags that are not well formed. In some cases it might not result in a visible issue and might not be very evident as well. Typically when these are opened in Visual studio it will highlight and the catch is when the html is huge or when it is dynamically rendered.
For ex. The business wanted to show a few text in bold, but that was not visible as bold to the users using that. Also the contents of the page beyond the error are not submitted back. Consider the snippet below
...
<table>
<thead>
<tr><td><b>Employee ID<\td><td><b>Employee ID<\b><\td><\tr>
</thead>
</table>
<input type="text" id="txtEmpName"/>
....
<input type="submit" value="Save"/>
....
You can expect issues that the text or other controls below are not part of the request collection. IE9 breaks on all errors and anything beyond the error don’t find a place in the list of values posted back. This might not be the case with IE8< browsers or on IE9 compatibility modes.

Solution:
The solution is to identify all HTML issues and ensure that the HTML is well formed(example: closing tags are present). This applies to Script tags as well.

Tuesday, October 14, 2014

I am trying to deserialize an XML file with a List<object> into a DataContract class. With the below code I am able to deserialize every property, except the List<object>.
 
Code:
    [DataContract(Namespace = "http://myNamespace")]
    [KnownType(typeof(List<Child>))]
    public class Person
    {
        [DataMember]
        public List<Child> Children { setget; }
        [DataMember]
        public DateTime birthdate { setget; }
        [DataMember]
        public string SSN { setget; }
        [DataMember]
        public string Gender { setget; }
        [DataMember]
        public string Name { setget; }
        [DataMember]
        public int PersonID { setget; }
 
        public Person()
        {
                Children=new List<Child>();
        }
    }
 
    [DataContract]
    public class Child
    {
        [DataMember]
        public string Name { setget; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var rm = new ResourceManager("WCFExample.Resource1"Assembly.GetExecutingAssembly());
            string xml = rm.GetString("Person.xml");
            Person r = null;
            var ms = new MemoryStream(Encoding.Default.GetBytes(xml));
            using (var reader = XmlDictionaryReader.CreateTextReader(ms, new XmlDictionaryReaderQuotas()))
            {
                DataContractSerializer ser = new DataContractSerializer(typeof(Person));
                r = (Person)ser.ReadObject(reader, true);
                reader.Close();
            }
            Console.WriteLine(  "Child name:"+r.Children[0].Name);
            Console.WriteLine( "PersonID:"+r.PersonID );
            Console.WriteLine(  "Navn:"+r.Name);
}
    }
 
XML file      (Person.xml):
 
<Person xmlns="http://myNamespace">
       <Children>
              <Child>
              <Name>chinnu</Name>
              </Child>
              <Child>
              <Name>bittu</Name>
              </Child>
       </Children>
      <birthdate>1980-12-31</birthdate>
      <SSN>10067136132</SSN>
      <Gender>M</Gender>
      <Name>Ravi</Name>
      <PersonID>1</PersonID>
    </Person>
 
Output: Though I have 2 child objects as shown in above XML, still the Children collection is always empty. So Children[0].Name is throwing exception. Not sure what I am missing here. Please help!


SOLUTION: 1

Replace:
    [DataContract]
    public class Child
    {
        [DataMember]
        public string Name { setget; }
    }
With
    [DataContract(Namespace = "http://myNamespace")]
    public class Child
    {
        [DataMember]
        public string Name { setget; }
    }
 
Using the same namespace on Child element also. However, it works only if namespaces are the same. I would love to know how to handle if both Person and Child are in different namespaces J



Friday, September 5, 2014

Need a suggestion in inserting a row into SQL from a XML document.

 
Let’s say this is the XML that I have
 
<Request>
    <Source>Hello</Source>
    <ProductID>678</ProductID>
    <FormID>GHjd</FormID>
    <Copies>1</Copies>
<Request/>
 
What Am presently doing for building the insert command is
 
        public string BuildQueryString(XElement Xdoc, out CustomException cust)
        {
            string query;
            string xml = Util.GetXelementAsString(Xdoc);
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.LoadXml(xml);
            XDocument xDoc = XDocument.Parse(xml);
            XElement Xelement = XElement.Parse(xml);
            query = "insert into LPI_RequestTable ";
            string columnquery = string.Empty;
            string valuesquery = string.Empty;
            var columns = from c in xDoc.Elements()
                          select c;
            foreach (var column in columns)
            {
                if (column.Elements("Source").Any())
                {
                    if (!(column.Element("Source").Value == null))
                    {
                        columnquery = columnquery + "LCM_Source,";
                        valuesquery = valuesquery + "'" + column.Element("Source").Value + "'" + ",";
                    }
                }
                if (column.Elements("ProductID").Any())
                {
                    if (!(column.Element("ProductID").Value == null))
                    {
                        columnquery = columnquery + "ProductID,";
                        valuesquery = valuesquery + "'" + column.Element("ProductID").Value + "'" + ",";
                    }
                }
                if (column.Elements("FormID").Any())
                {
                    if (!(column.Element("FormID").Value == null))
                    {
                        columnquery = columnquery + "FormID,";
                        valuesquery = valuesquery + "'" + column.Element("FormID").Value + '" + ",";
                    }
                }
                if (column.Elements("Copies").Any())
                {
                    if (!(column.Element("Copies").Value == null))
                    {
                        columnquery = columnquery + "Copies,";
                        valuesquery = valuesquery + column.Element("Copies").Value + ",";
                    }
                }
            }
 
            query = query + "(" + columnquery + ")" + " Values (" + valuesquery + ");";
            return query;
 
        }
 
Problem here is My client doesn’t need the hard coding to be done in building the command. Could anybody help me by providing something which doesn’t need a hard coding.