Skip to content

Commit

Permalink
Add support for xml: namespaced properties (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcodes authored Jul 23, 2018
1 parent 9a17435 commit 465d3c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/Twilio/TwiML/TwiML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using System.Xml.Linq;
using Twilio.Exceptions;

namespace Twilio.TwiML
namespace Twilio.TwiML
{

/// <summary>
/// Base class for all TwiML Objects.
/// </summary>
public class TwiML
public class TwiML
{
/// <summary>
/// Tag name
Expand Down Expand Up @@ -78,7 +78,7 @@ public T Nest<T>(T childElem) where T : TwiML
this.Children.Add(childElem);
return childElem;
}

/// <summary>
/// Add a generic child TwiML object
/// </summary>
Expand Down Expand Up @@ -121,7 +121,20 @@ protected virtual XNode ToXml()
var elem = new XElement(this.TagName, this.GetElementBody());

this.GetElementAttributes().ForEach(attr => elem.Add(attr));
this.Options.ForEach(e => elem.Add(new XAttribute(e.Key, e.Value)));

this.Options.ForEach(e =>
{
if (e.Key.StartsWith("xml:"))
{
var attribute = new XAttribute(XNamespace.Xml + e.Key.Substring(4), e.Value);
elem.Add(attribute);
}
else
{
elem.Add(new XAttribute(e.Key, e.Value));
}
});

this.Children.ForEach(child => elem.Add(child.ToXml()));

return elem;
Expand All @@ -146,7 +159,7 @@ public string ToString(SaveOptions formattingOptions = SaveOptions.None)
/// <summary>
/// StringWriter which overrides default encoding to use UTF8.
/// </summary>
public class Utf8StringWriter : StringWriter
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
Expand Down
17 changes: 15 additions & 2 deletions test/Twilio.Test/TwiML/BodyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
using Twilio.Converters;
using Twilio.TwiML.Messaging;

namespace Twilio.Tests.TwiML
namespace Twilio.Tests.TwiML
{

[TestFixture]
public class BodyTest : TwilioTest
public class BodyTest : TwilioTest
{
[Test]
public void TestEmptyElement()
Expand Down Expand Up @@ -94,6 +94,19 @@ public void TestMixedContent()
elem.ToString()
);
}

[Test]
public void TestXmlNamespace()
{
var elem = new Body();
elem.SetOption("xml:lang", "en-US");

Assert.AreEqual(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine +
"<Body xml:lang=\"en-US\"></Body>",
elem.ToString()
);
}
}

}

0 comments on commit 465d3c5

Please sign in to comment.