-
-
Notifications
You must be signed in to change notification settings - Fork 105
XML _ XElementFormatter
axunonb edited this page Mar 11, 2022
·
4 revisions
The XElementFormatter
formats and outputs XElement
s.
{ XElement : xml }
Value | formatter name |
---|---|
XElement | "xml" |
Note:
- There are no "option" or "format" arguments to the
XElementFormatter
. - Supports XML with namespaces.
none
// Process single level xml
var oneLevel= XElement.Parse(
"<root>" +
"<FirstName>Joe</FirstName>" +
"<LastName>Doe</LastName>" +
"<Dob>1950-05-05</Dob>" +
"</root>");
var smart = Smart.CreateDefaultSmartFormat()
.AddExtensions(new XElementFormatter())
.AddExtensions(new XmlSource());
smart.Format("Mr {FirstName:xml} {LastName:xml}", oneLevel);
// Outputs: "Mr Joe Doe"
var twoLevel = XElement.Parse(
"<root>" +
"<Person>" +
"<FirstName>Joe</FirstName>" +
"<LastName>Doe</LastName>" +
"<Phone>123-123-1234</Phone>" +
"</Person>" +
"<Person>" +
"<FirstName>Jack</FirstName>" +
"<LastName>Doe</LastName>" +
"<Phone>789-789-7890</Phone>" +
"</Person>" +
"</root>");
var smart = Smart.CreateDefaultSmartFormat()
.AddExtensions(new XElementFormatter())
.AddExtensions(new XmlSource());
// Access xml level by index
smart.Format("{Person[1].FirstName:xml}, Phone: {Person[1].Phone:xml}", twoLevel);
// Outputs: "Jack, Phone: 789-789-7890"
var multiFirstName = XElement.Parse(
"<root>" +
"<FirstName>Joe</FirstName>" +
"<FirstName>Jack</FirstName>" +
"<LastName>Doe</LastName>" +
"<FirstName>Jim</FirstName>" +
"</root>");
var smart = Smart.CreateDefaultSmartFormat()
.AddExtensions(new XElementFormatter())
.AddExtensions(new XmlSource());
// More xml elements with the same name are treated as a list
// (used together with ConditionalFormatter)
smart.Format("There {FirstName.Count:cond:is {} Doe |are {} Does}", multiFirstName);
// Outputs: "There are 3 Does"
// List FirstNames with ListFormatter
smart.Format("{FirstName:list:|, | and }", multiFirstName);
// Outputs: "Joe, Jack and Jim"
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License