Przestrzenie nazw XElement (jak?)
Jak utworzyć dokument xml z prefiksem węzła jak:
<sphinx:docset>
<sphinx:schema>
<sphinx:field name="subject"/>
<sphinx:field name="content"/>
<sphinx:attr name="published" type="timestamp"/>
</sphinx:schema>
Kiedy próbuję uruchomić coś takiego new XElement("sphinx:docset")
dostaję wyjątek
Nieobsługiwany Wyjątek: System.Xml.XmlException: znak":", szesnastkowy val ue 0x3a, nie może być zawarta w nazwie.
system at.Xml.XmlConvert.VerifyNCName( String name, ExceptionType exceptionTyp e)
system at.Xml.Linq.XName..ctor (XNamespace ns, String localName)
na System.Xml.Linq.XNamespace.GetName (String localName)
system at.Xml.Linq.XName.Get (String expandedName)
67
Author: Micha Wiedenmann, 2011-02-13
2 answers
W LINQ do XML jest to naprawdę proste:
XNamespace ns = "sphinx";
XElement element = new XElement(ns + "docset");
Lub aby "alias" działał poprawnie, aby wyglądał jak twoje przykłady, coś takiego:
XNamespace ns = "http://url/for/sphinx";
XElement element = new XElement("container",
new XAttribute(XNamespace.Xmlns + "sphinx", ns),
new XElement(ns + "docset",
new XElement(ns + "schema"),
new XElement(ns + "field", new XAttribute("name", "subject")),
new XElement(ns + "field", new XAttribute("name", "content")),
new XElement(ns + "attr",
new XAttribute("name", "published"),
new XAttribute("type", "timestamp"))));
Który daje:
<container xmlns:sphinx="http://url/for/sphinx">
<sphinx:docset>
<sphinx:schema />
<sphinx:field name="subject" />
<sphinx:field name="content" />
<sphinx:attr name="published" type="timestamp" />
</sphinx:docset>
</container>
105
Author: Jon Skeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-13 18:31:56
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-13 18:31:56
Możesz odczytać przestrzeń nazw dokumentu i użyć jej w zapytaniach takich jak:
XDocument xml = XDocument.Load(address);
XNamespace ns = xml.Root.Name.Namespace;
foreach (XElement el in xml.Descendants(ns + "whateverYourElementNameIs"))
//do stuff
20
Author: Adam Rackis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-13 18:35:07
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-13 18:35:07