PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs

https://bitbucket.org/danipen/mono
C# | 843 lines | 692 code | 120 blank | 31 comment | 207 complexity | 0f89b02c12725ec337942f7436c412ab MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Authors:
  3. // Atsushi Enomoto
  4. //
  5. // Copyright 2007 Novell (http://www.novell.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Linq;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.Serialization;
  35. namespace System.Xml.Linq
  36. {
  37. [XmlSchemaProvider (null, IsAny = true)]
  38. public class XElement : XContainer, IXmlSerializable
  39. {
  40. static IEnumerable <XElement> emptySequence =
  41. new List <XElement> ();
  42. public static IEnumerable <XElement> EmptySequence {
  43. get { return emptySequence; }
  44. }
  45. XName name;
  46. XAttribute attr_first, attr_last;
  47. bool explicit_is_empty = true;
  48. public XElement (XName name, object content)
  49. {
  50. if (name == null)
  51. throw new ArgumentNullException ("name");
  52. this.name = name;
  53. Add (content);
  54. }
  55. public XElement (XElement other)
  56. {
  57. if (other == null)
  58. throw new ArgumentNullException ("other");
  59. name = other.name;
  60. Add (other.Attributes ());
  61. Add (other.Nodes ());
  62. }
  63. public XElement (XName name)
  64. {
  65. if (name == null)
  66. throw new ArgumentNullException ("name");
  67. this.name = name;
  68. }
  69. public XElement (XName name, params object [] content)
  70. {
  71. if (name == null)
  72. throw new ArgumentNullException ("name");
  73. this.name = name;
  74. Add (content);
  75. }
  76. public XElement (XStreamingElement other)
  77. {
  78. if (other == null)
  79. throw new ArgumentNullException ("other");
  80. this.name = other.Name;
  81. Add (other.Contents);
  82. }
  83. [CLSCompliant (false)]
  84. public static explicit operator bool (XElement element)
  85. {
  86. if (element == null)
  87. throw new ArgumentNullException ("element");
  88. return XUtil.ConvertToBoolean (element.Value);
  89. }
  90. [CLSCompliant (false)]
  91. public static explicit operator bool? (XElement element)
  92. {
  93. if (element == null)
  94. return null;
  95. return element.Value == null ? (bool?) null : XUtil.ConvertToBoolean (element.Value);
  96. }
  97. [CLSCompliant (false)]
  98. public static explicit operator DateTime (XElement element)
  99. {
  100. if (element == null)
  101. throw new ArgumentNullException ("element");
  102. return XUtil.ToDateTime (element.Value);
  103. }
  104. [CLSCompliant (false)]
  105. public static explicit operator DateTime? (XElement element)
  106. {
  107. if (element == null)
  108. return null;
  109. return element.Value == null ? (DateTime?) null : XUtil.ToDateTime (element.Value);
  110. }
  111. #if !TARGET_JVM // Same as for System.Xml.XmlConvert.ToDateTimeOffset
  112. [CLSCompliant (false)]
  113. public static explicit operator DateTimeOffset (XElement element)
  114. {
  115. if (element == null)
  116. throw new ArgumentNullException ("element");
  117. return XmlConvert.ToDateTimeOffset (element.Value);
  118. }
  119. [CLSCompliant (false)]
  120. public static explicit operator DateTimeOffset? (XElement element)
  121. {
  122. if (element == null)
  123. return null;
  124. return element.Value == null ? (DateTimeOffset?) null : XmlConvert.ToDateTimeOffset (element.Value);
  125. }
  126. #endif
  127. [CLSCompliant (false)]
  128. public static explicit operator decimal (XElement element)
  129. {
  130. if (element == null)
  131. throw new ArgumentNullException ("element");
  132. return XmlConvert.ToDecimal (element.Value);
  133. }
  134. [CLSCompliant (false)]
  135. public static explicit operator decimal? (XElement element)
  136. {
  137. if (element == null)
  138. return null;
  139. return element.Value == null ? (decimal?) null : XmlConvert.ToDecimal (element.Value);
  140. }
  141. [CLSCompliant (false)]
  142. public static explicit operator double (XElement element)
  143. {
  144. if (element == null)
  145. throw new ArgumentNullException ("element");
  146. return XmlConvert.ToDouble (element.Value);
  147. }
  148. [CLSCompliant (false)]
  149. public static explicit operator double? (XElement element)
  150. {
  151. if (element == null)
  152. return null;
  153. return element.Value == null ? (double?) null : XmlConvert.ToDouble (element.Value);
  154. }
  155. [CLSCompliant (false)]
  156. public static explicit operator float (XElement element)
  157. {
  158. if (element == null)
  159. throw new ArgumentNullException ("element");
  160. return XmlConvert.ToSingle (element.Value);
  161. }
  162. [CLSCompliant (false)]
  163. public static explicit operator float? (XElement element)
  164. {
  165. if (element == null)
  166. return null;
  167. return element.Value == null ? (float?) null : XmlConvert.ToSingle (element.Value);
  168. }
  169. [CLSCompliant (false)]
  170. public static explicit operator Guid (XElement element)
  171. {
  172. if (element == null)
  173. throw new ArgumentNullException ("element");
  174. return XmlConvert.ToGuid (element.Value);
  175. }
  176. [CLSCompliant (false)]
  177. public static explicit operator Guid? (XElement element)
  178. {
  179. if (element == null)
  180. return null;
  181. return element.Value == null ? (Guid?) null : XmlConvert.ToGuid (element.Value);
  182. }
  183. [CLSCompliant (false)]
  184. public static explicit operator int (XElement element)
  185. {
  186. if (element == null)
  187. throw new ArgumentNullException ("element");
  188. return XmlConvert.ToInt32 (element.Value);
  189. }
  190. [CLSCompliant (false)]
  191. public static explicit operator int? (XElement element)
  192. {
  193. if (element == null)
  194. return null;
  195. return element.Value == null ? (int?) null : XmlConvert.ToInt32 (element.Value);
  196. }
  197. [CLSCompliant (false)]
  198. public static explicit operator long (XElement element)
  199. {
  200. if (element == null)
  201. throw new ArgumentNullException ("element");
  202. return XmlConvert.ToInt64 (element.Value);
  203. }
  204. [CLSCompliant (false)]
  205. public static explicit operator long? (XElement element)
  206. {
  207. if (element == null)
  208. return null;
  209. return element.Value == null ? (long?) null : XmlConvert.ToInt64 (element.Value);
  210. }
  211. [CLSCompliant (false)]
  212. public static explicit operator uint (XElement element)
  213. {
  214. if (element == null)
  215. throw new ArgumentNullException ("element");
  216. return XmlConvert.ToUInt32 (element.Value);
  217. }
  218. [CLSCompliant (false)]
  219. public static explicit operator uint? (XElement element)
  220. {
  221. if (element == null)
  222. return null;
  223. return element.Value == null ? (uint?) null : XmlConvert.ToUInt32 (element.Value);
  224. }
  225. [CLSCompliant (false)]
  226. public static explicit operator ulong (XElement element)
  227. {
  228. if (element == null)
  229. throw new ArgumentNullException ("element");
  230. return XmlConvert.ToUInt64 (element.Value);
  231. }
  232. [CLSCompliant (false)]
  233. public static explicit operator ulong? (XElement element)
  234. {
  235. if (element == null)
  236. return null;
  237. return element.Value == null ? (ulong?) null : XmlConvert.ToUInt64 (element.Value);
  238. }
  239. [CLSCompliant (false)]
  240. public static explicit operator TimeSpan (XElement element)
  241. {
  242. if (element == null)
  243. throw new ArgumentNullException ("element");
  244. return XmlConvert.ToTimeSpan (element.Value);
  245. }
  246. [CLSCompliant (false)]
  247. public static explicit operator TimeSpan? (XElement element)
  248. {
  249. if (element == null)
  250. return null;
  251. return element.Value == null ? (TimeSpan?) null : XmlConvert.ToTimeSpan (element.Value);
  252. }
  253. [CLSCompliant (false)]
  254. public static explicit operator string (XElement element)
  255. {
  256. if (element == null)
  257. return null;
  258. return element.Value;
  259. }
  260. public XAttribute FirstAttribute {
  261. get { return attr_first; }
  262. internal set { attr_first = value; }
  263. }
  264. public XAttribute LastAttribute {
  265. get { return attr_last; }
  266. internal set { attr_last = value; }
  267. }
  268. public bool HasAttributes {
  269. get { return attr_first != null; }
  270. }
  271. public bool HasElements {
  272. get {
  273. foreach (object o in Nodes ())
  274. if (o is XElement)
  275. return true;
  276. return false;
  277. }
  278. }
  279. public bool IsEmpty {
  280. get { return !Nodes ().GetEnumerator ().MoveNext () && explicit_is_empty; }
  281. internal set { explicit_is_empty = value; }
  282. }
  283. public XName Name {
  284. get { return name; }
  285. set {
  286. if (value == null)
  287. throw new ArgumentNullException ("Name");
  288. OnNameChanging (this);
  289. name = value;
  290. OnNameChanged (this);
  291. }
  292. }
  293. public override XmlNodeType NodeType {
  294. get { return XmlNodeType.Element; }
  295. }
  296. public string Value {
  297. get {
  298. StringBuilder sb = null;
  299. foreach (XNode n in Nodes ()) {
  300. if (sb == null)
  301. sb = new StringBuilder ();
  302. if (n is XText)
  303. sb.Append (((XText) n).Value);
  304. else if (n is XElement)
  305. sb.Append (((XElement) n).Value);
  306. }
  307. return sb == null ? String.Empty : sb.ToString ();
  308. }
  309. set {
  310. RemoveNodes ();
  311. Add (value);
  312. }
  313. }
  314. IEnumerable <XElement> GetAncestorList (XName name, bool getMeIn)
  315. {
  316. List <XElement> list = new List <XElement> ();
  317. if (getMeIn)
  318. list.Add (this);
  319. for (XElement el = Parent as XElement; el != null; el = el.Parent as XElement)
  320. if (name == null || el.Name == name)
  321. list.Add (el);
  322. return list;
  323. }
  324. public XAttribute Attribute (XName name)
  325. {
  326. foreach (XAttribute a in Attributes ())
  327. if (a.Name == name)
  328. return a;
  329. return null;
  330. }
  331. public IEnumerable <XAttribute> Attributes ()
  332. {
  333. XAttribute next;
  334. for (XAttribute a = attr_first; a != null; a = next) {
  335. next = a.NextAttribute;
  336. yield return a;
  337. }
  338. }
  339. // huh?
  340. public IEnumerable <XAttribute> Attributes (XName name)
  341. {
  342. foreach (XAttribute a in Attributes ())
  343. if (a.Name == name)
  344. yield return a;
  345. }
  346. static void DefineDefaultSettings (XmlReaderSettings settings, LoadOptions options)
  347. {
  348. settings.ProhibitDtd = false;
  349. settings.IgnoreWhitespace = (options & LoadOptions.PreserveWhitespace) == 0;
  350. }
  351. static XmlReaderSettings CreateDefaultSettings (LoadOptions options)
  352. {
  353. var settings = new XmlReaderSettings ();
  354. DefineDefaultSettings (settings, options);
  355. return settings;
  356. }
  357. public static XElement Load (string uri)
  358. {
  359. return Load (uri, LoadOptions.None);
  360. }
  361. public static XElement Load (string uri, LoadOptions options)
  362. {
  363. XmlReaderSettings s = CreateDefaultSettings (options);
  364. using (XmlReader r = XmlReader.Create (uri, s)) {
  365. return LoadCore (r, options);
  366. }
  367. }
  368. public static XElement Load (TextReader textReader)
  369. {
  370. return Load (textReader, LoadOptions.None);
  371. }
  372. public static XElement Load (TextReader textReader, LoadOptions options)
  373. {
  374. XmlReaderSettings s = CreateDefaultSettings (options);
  375. using (XmlReader r = XmlReader.Create (textReader, s)) {
  376. return LoadCore (r, options);
  377. }
  378. }
  379. public static XElement Load (XmlReader reader)
  380. {
  381. return Load (reader, LoadOptions.None);
  382. }
  383. public static XElement Load (XmlReader reader, LoadOptions options)
  384. {
  385. XmlReaderSettings s = reader.Settings != null ? reader.Settings.Clone () : new XmlReaderSettings ();
  386. DefineDefaultSettings (s, options);
  387. using (XmlReader r = XmlReader.Create (reader, s)) {
  388. return LoadCore (r, options);
  389. }
  390. }
  391. #if NET_4_0
  392. public static XElement Load (Stream stream)
  393. {
  394. return Load (stream, LoadOptions.None);
  395. }
  396. public static XElement Load (Stream stream, LoadOptions options)
  397. {
  398. XmlReaderSettings s = new XmlReaderSettings ();
  399. DefineDefaultSettings (s, options);
  400. using (XmlReader r = XmlReader.Create (stream, s)) {
  401. return LoadCore (r, options);
  402. }
  403. }
  404. #endif
  405. internal static XElement LoadCore (XmlReader r, LoadOptions options)
  406. {
  407. r.MoveToContent ();
  408. if (r.NodeType != XmlNodeType.Element)
  409. throw new InvalidOperationException ("The XmlReader must be positioned at an element");
  410. XName name = XName.Get (r.LocalName, r.NamespaceURI);
  411. XElement e = new XElement (name);
  412. e.FillLineInfoAndBaseUri (r, options);
  413. if (r.MoveToFirstAttribute ()) {
  414. do {
  415. // not sure how current Orcas behavior makes sense here though ...
  416. if (r.LocalName == "xmlns" && r.NamespaceURI == XNamespace.Xmlns.NamespaceName)
  417. e.SetAttributeValue (XNamespace.None.GetName ("xmlns"), r.Value);
  418. else
  419. e.SetAttributeValue (XName.Get (r.LocalName, r.NamespaceURI), r.Value);
  420. e.LastAttribute.FillLineInfoAndBaseUri (r, options);
  421. } while (r.MoveToNextAttribute ());
  422. r.MoveToElement ();
  423. }
  424. if (!r.IsEmptyElement) {
  425. r.Read ();
  426. e.ReadContentFrom (r, options);
  427. r.ReadEndElement ();
  428. e.explicit_is_empty = false;
  429. } else {
  430. e.explicit_is_empty = true;
  431. r.Read ();
  432. }
  433. return e;
  434. }
  435. public static XElement Parse (string text)
  436. {
  437. return Parse (text, LoadOptions.None);
  438. }
  439. public static XElement Parse (string text, LoadOptions options)
  440. {
  441. return Load (new StringReader (text), options);
  442. }
  443. public void RemoveAll ()
  444. {
  445. RemoveAttributes ();
  446. RemoveNodes ();
  447. }
  448. public void RemoveAttributes ()
  449. {
  450. while (attr_first != null)
  451. attr_last.Remove ();
  452. }
  453. public void Save (string fileName)
  454. {
  455. Save (fileName, SaveOptions.None);
  456. }
  457. public void Save (string fileName, SaveOptions options)
  458. {
  459. XmlWriterSettings s = new XmlWriterSettings ();
  460. if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
  461. s.Indent = true;
  462. #if NET_4_0
  463. if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
  464. s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
  465. #endif
  466. using (XmlWriter w = XmlWriter.Create (fileName, s)) {
  467. Save (w);
  468. }
  469. }
  470. public void Save (TextWriter textWriter)
  471. {
  472. Save (textWriter, SaveOptions.None);
  473. }
  474. public void Save (TextWriter textWriter, SaveOptions options)
  475. {
  476. XmlWriterSettings s = new XmlWriterSettings ();
  477. if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
  478. s.Indent = true;
  479. #if NET_4_0
  480. if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
  481. s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
  482. #endif
  483. using (XmlWriter w = XmlWriter.Create (textWriter, s)) {
  484. Save (w);
  485. }
  486. }
  487. public void Save (XmlWriter writer)
  488. {
  489. WriteTo (writer);
  490. }
  491. #if NET_4_0
  492. public void Save (Stream stream)
  493. {
  494. Save (stream, SaveOptions.None);
  495. }
  496. public void Save (Stream stream, SaveOptions options)
  497. {
  498. XmlWriterSettings s = new XmlWriterSettings ();
  499. if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
  500. s.Indent = true;
  501. if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
  502. s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
  503. using (var writer = XmlWriter.Create (stream, s)){
  504. Save (writer);
  505. }
  506. }
  507. #endif
  508. public IEnumerable <XElement> AncestorsAndSelf ()
  509. {
  510. return GetAncestorList (null, true);
  511. }
  512. public IEnumerable <XElement> AncestorsAndSelf (XName name)
  513. {
  514. return GetAncestorList (name, true);
  515. }
  516. public IEnumerable <XElement> DescendantsAndSelf ()
  517. {
  518. List <XElement> list = new List <XElement> ();
  519. list.Add (this);
  520. list.AddRange (Descendants ());
  521. return list;
  522. }
  523. public IEnumerable <XElement> DescendantsAndSelf (XName name)
  524. {
  525. List <XElement> list = new List <XElement> ();
  526. if (name == this.name)
  527. list.Add (this);
  528. list.AddRange (Descendants (name));
  529. return list;
  530. }
  531. public IEnumerable <XNode> DescendantNodesAndSelf ()
  532. {
  533. yield return this;
  534. foreach (XNode node in DescendantNodes ())
  535. yield return node;
  536. }
  537. public void SetAttributeValue (XName name, object value)
  538. {
  539. XAttribute a = Attribute (name);
  540. if (value == null) {
  541. if (a != null)
  542. a.Remove ();
  543. } else {
  544. if (a == null) {
  545. SetAttributeObject (new XAttribute (name, value));
  546. }
  547. else
  548. a.Value = XUtil.ToString (value);
  549. }
  550. }
  551. void SetAttributeObject (XAttribute a)
  552. {
  553. OnAddingObject (a);
  554. a = (XAttribute) XUtil.GetDetachedObject (a);
  555. a.SetOwner (this);
  556. if (attr_first == null) {
  557. attr_first = a;
  558. attr_last = a;
  559. } else {
  560. attr_last.NextAttribute = a;
  561. a.PreviousAttribute = attr_last;
  562. attr_last = a;
  563. }
  564. OnAddedObject (a);
  565. }
  566. string LookupPrefix (string ns, XmlWriter w)
  567. {
  568. string prefix = ns.Length > 0 ? GetPrefixOfNamespace (ns) ?? w.LookupPrefix (ns) : String.Empty;
  569. foreach (XAttribute a in Attributes ()) {
  570. if (a.IsNamespaceDeclaration && a.Value == ns) {
  571. if (a.Name.Namespace == XNamespace.Xmlns)
  572. prefix = a.Name.LocalName;
  573. // otherwise xmlns="..."
  574. break;
  575. }
  576. }
  577. return prefix;
  578. }
  579. static string CreateDummyNamespace (ref int createdNS, IEnumerable<XAttribute> atts, bool isAttr)
  580. {
  581. if (!isAttr && atts.All (a => a.Name.LocalName != "xmlns" || a.Name.NamespaceName == XNamespace.Xmlns.NamespaceName))
  582. return String.Empty;
  583. string p = null;
  584. do {
  585. p = "p" + (++createdNS);
  586. // check conflict
  587. if (atts.All (a => a.Name.LocalName != p || a.Name.NamespaceName == XNamespace.Xmlns.NamespaceName))
  588. break;
  589. } while (true);
  590. return p;
  591. }
  592. public override void WriteTo (XmlWriter writer)
  593. {
  594. // some people expect the same prefix output as in input,
  595. // in the loss of performance... see bug #466423.
  596. string prefix = LookupPrefix (name.NamespaceName, writer);
  597. int createdNS = 0;
  598. if (prefix == null)
  599. prefix = CreateDummyNamespace (ref createdNS, Attributes (), false);
  600. writer.WriteStartElement (prefix, name.LocalName, name.Namespace.NamespaceName);
  601. foreach (XAttribute a in Attributes ()) {
  602. if (a.IsNamespaceDeclaration) {
  603. if (a.Name.Namespace == XNamespace.Xmlns)
  604. writer.WriteAttributeString ("xmlns", a.Name.LocalName, XNamespace.Xmlns.NamespaceName, a.Value);
  605. else
  606. writer.WriteAttributeString ("xmlns", a.Value);
  607. } else {
  608. string apfix = LookupPrefix (a.Name.NamespaceName, writer);
  609. if (apfix == null)
  610. apfix = CreateDummyNamespace (ref createdNS, Attributes (), true);
  611. writer.WriteAttributeString (apfix, a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value);
  612. }
  613. }
  614. foreach (XNode node in Nodes ())
  615. node.WriteTo (writer);
  616. if (explicit_is_empty)
  617. writer.WriteEndElement ();
  618. else
  619. writer.WriteFullEndElement ();
  620. }
  621. public XNamespace GetDefaultNamespace ()
  622. {
  623. for (XElement el = this; el != null; el = el.Parent)
  624. foreach (XAttribute a in el.Attributes ())
  625. if (a.IsNamespaceDeclaration && a.Name.Namespace == XNamespace.None)
  626. return XNamespace.Get (a.Value);
  627. return XNamespace.None; // nothing is declared.
  628. }
  629. public XNamespace GetNamespaceOfPrefix (string prefix)
  630. {
  631. for (XElement el = this; el != null; el = el.Parent)
  632. foreach (XAttribute a in el.Attributes ())
  633. if (a.IsNamespaceDeclaration && (prefix.Length == 0 && a.Name.LocalName == "xmlns" || a.Name.LocalName == prefix))
  634. return XNamespace.Get (a.Value);
  635. return XNamespace.None; // nothing is declared.
  636. }
  637. public string GetPrefixOfNamespace (XNamespace ns)
  638. {
  639. foreach (string prefix in GetPrefixOfNamespaceCore (ns))
  640. if (GetNamespaceOfPrefix (prefix) == ns)
  641. return prefix;
  642. return null; // nothing is declared
  643. }
  644. IEnumerable<string> GetPrefixOfNamespaceCore (XNamespace ns)
  645. {
  646. for (XElement el = this; el != null; el = el.Parent)
  647. foreach (XAttribute a in el.Attributes ())
  648. if (a.IsNamespaceDeclaration && a.Value == ns.NamespaceName)
  649. yield return a.Name.Namespace == XNamespace.None ? String.Empty : a.Name.LocalName;
  650. }
  651. public void ReplaceAll (object content)
  652. {
  653. RemoveNodes ();
  654. Add (content);
  655. }
  656. public void ReplaceAll (params object [] content)
  657. {
  658. RemoveNodes ();
  659. Add (content);
  660. }
  661. public void ReplaceAttributes (object content)
  662. {
  663. RemoveAttributes ();
  664. Add (content);
  665. }
  666. public void ReplaceAttributes (params object [] content)
  667. {
  668. RemoveAttributes ();
  669. Add (content);
  670. }
  671. public void SetElementValue (XName name, object value)
  672. {
  673. var element = Element (name);
  674. if (element == null && value != null) {
  675. Add (new XElement (name, value));
  676. } else if (element != null && value == null) {
  677. element.Remove ();
  678. } else
  679. element.SetValue (value);
  680. }
  681. public void SetValue (object value)
  682. {
  683. if (value == null)
  684. throw new ArgumentNullException ("value");
  685. if (value is XAttribute || value is XDocument || value is XDeclaration || value is XDocumentType)
  686. throw new ArgumentException (String.Format ("Node type {0} is not allowed as element value", value.GetType ()));
  687. RemoveNodes ();
  688. foreach (object o in XUtil.ExpandArray (value))
  689. Add (o);
  690. }
  691. internal override bool OnAddingObject (object o, bool rejectAttribute, XNode refNode, bool addFirst)
  692. {
  693. if (o is XDocument || o is XDocumentType || o is XDeclaration || (rejectAttribute && o is XAttribute))
  694. throw new ArgumentException (String.Format ("A node of type {0} cannot be added as a content", o.GetType ()));
  695. XAttribute a = o as XAttribute;
  696. if (a != null) {
  697. foreach (XAttribute ia in Attributes ())
  698. if (a.Name == ia.Name)
  699. throw new InvalidOperationException (String.Format ("Duplicate attribute: {0}", a.Name));
  700. SetAttributeObject (a);
  701. return true;
  702. }
  703. else if (o is string && refNode is XText) {
  704. ((XText) refNode).Value += o as string;
  705. return true;
  706. }
  707. else
  708. return false;
  709. }
  710. void IXmlSerializable.WriteXml (XmlWriter writer)
  711. {
  712. Save (writer);
  713. }
  714. void IXmlSerializable.ReadXml (XmlReader reader)
  715. {
  716. ReadContentFrom (reader, LoadOptions.None);
  717. }
  718. XmlSchema IXmlSerializable.GetSchema ()
  719. {
  720. return null;
  721. }
  722. }
  723. }