0

I am trying to change the XElement XName to a readable format, but cant. How to replace the XElement name with an alias name? Below is my XElement code.

    var root = new XElement("Reports",
                                        from l in _userReportMasterDataList
                                        join m in _userReportLocalDataList on l.UsrID equals m.TypeValue
                                        select new XElement("Report",
                                        new XElement("CreatedDate", (DateTime)(l.CreatedDate)),
                                        new XElement("LogType", (string)(l.LogType)),
                                        new XElement("LoginID", (string)(l.LoginID)),
                                        new XElement("Name", (string)(m.Name)),
                                        new XElement("AppVersion", (string)(l.AppVersion)),
                                        new XElement("System", (string)(l.System)),
                                        new XElement("UserIP", (string)(l.UserIPAddress)),
                                        new XElement("LoginDate", (DateTime?)(l.LoginDate)),
                                        new XElement("LogoutDate", (DateTime?)(l.LogoutDate)),
                                        new XElement("Remarks", (string)(l.Remarks))), new XElement("Header",
                                        new XElement("ReportID", (string)(r.ReportID)),
                                        new XElement("GroupingColumn1", (string)(r.GroupingColumn1)),
                                        new XElement("PrintedBy", (string)(r.ReqHdr.Server)),
                                        new XElement("PrintedDate", (DateTime)(DateTime.UtcNow))));

In the place of the XName "CreatedDate", I want to replace it with Date Created, "UserIP" to "IP Address" etc.

Thanks,

  • 1
    XElement name follow XML standard. It can't contain space on the element name. but you can use attribute to put a description of the element name. – CodeNotFound Aug 18 '16 at 05:35
  • @CodeNotFound : How can i use the attribute wrt the above code? Can you please post an example. Thanks! – Pavan Puligandla Aug 18 '16 at 06:32
  • Just a new XAttribute ot each XElement constructor you need. – CodeNotFound Aug 18 '16 at 07:12
  • @CodeNotFound : I did that, But the names didnt change, rather it simply added another element. I want to change the XElement names to end user understandable format with spaces. – Pavan Puligandla Aug 18 '16 at 07:37
  • XML element names cannot contain spaces. See https://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name. You can use [`XmlConvert.EncodeLocalName(String)`](https://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.encodelocalname(v=vs.110).aspx) to encode a name with spaces, then decode them later. – dbc Aug 18 '16 at 07:49
  • @dbc: That worked well.. A big thanks for you.. Really saved me.. I am now encoding the string of my choice and decoding it in the XSLT template.. Thanks a ton!! – Pavan Puligandla Aug 18 '16 at 10:08

1 Answers1

1

Answered by @dbc:

XML element names cannot contain spaces. See stackoverflow.com/questions/2519845/…. You can use XmlConvert.EncodeLocalName(String) to encode a name with spaces, then decode them later.

Community
  • 1
  • 1