Benutzer mit den meisten Antworten
Problem mit XML

Frage
-
Hallo NG,
ich hatte diese Anfrage hier schon einmal gestellt, leider wurde diese während meines Urlaubs geschlossen. Ich hatte leider keine Internetverbindung. Kommt hier in Brasilien leider öfter vor wenn man sich nicht in großen Städten aufhält.
Nun noch einmal zu meinem Problem.
Ich möchte folgendes Attribut/Namespace in meinem XML Dokument haben:
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd">
Mein letzter Versuch war:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlElement rootNode = doc.CreateElement("Document");
XmlAttribute att = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
att.Value = "urn:iso:std:iso:20022:tech:xsd:pain.008.002.02 pain.008.002.02.xsd";
rootNode.SetAttributeNode(att);
doc.AppendChild(rootNode);Heraus gekommen ist leider:
<Document xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.008.002.02 pain.008.002.02.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Kann mir jemand helfen???
Danke im voraus für jeden Hinweis und Tipp.
Grüße Ingo
Antworten
-
Hallo zusammen,
Die Frage wurde anscheinend mehrfach und in verschiedenen Foren gepostet. Viele interessante Lösungsvorschläge wurden unterbreitet, weshalb mir nicht ganz klar ist, wo das Problem liegt. Auf codekicker z.B. hatte Maria Simlinger am 12.09 vorgeschlagen, dass man der CreateAttribute-Methode die Namespace-URI direkt mitgibt, was tatsächlich funktioniert:
XmlElement rootNode = doc.CreateElement("Document"); XmlElement xRootElement = (XmlElement)doc.AppendChild(rootNode); xRootElement.SetAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:pain.001.002.03"); xRootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlAttribute schemaLocationAttribute = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); schemaLocationAttribute.Value = "urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd"; xRootElement.Attributes.Append(schemaLocationAttribute);
Hier ist die XML-Ausgabe von doc.OuterXml:
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd" />
Gruß
Marcel
- Als Antwort markiert IngoManthey Dienstag, 24. September 2013 21:44
Alle Antworten
-
Hallo Ingo,
schau dir mal diese Beispiele an.
EDIT: Ich habe deinen Code ergänzt, damit das gewünschte Ergebnis ausgegeben wird.
XmlDocument xmlDoc = new XmlDocument(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace(string.Empty, "urn:iso:std:iso:20022:tech:xsd:pain.001.002.03"); nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlElement rootNode = xmlDoc.CreateElement("Document", nsmgr.LookupNamespace(string.Empty)); XmlAttribute xmlSchema = xmlDoc.CreateAttribute("xsi", "schemaLocation", nsmgr.LookupNamespace("xsi")); xmlSchema.Value = "urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd"; rootNode.Attributes.Append(xmlSchema); xmlDoc.AppendChild(rootNode); xmlDoc.Save(Console.Out); Console.ReadLine();
Du kannst, statt nsmgr.LookupNamespace zu verwenden, die Namespaces auch in Konstanten ablegen und dann sowohl bei nsmgr.AddNamespace als auch bei xmlDoc.CreateElement bzw. xmlDoc.CreateAttribute verwenden.
Gruss,
LBB
- Bearbeitet LittleBlueBird Montag, 23. September 2013 19:39 Nachtrag
- Als Antwort vorgeschlagen LittleBlueBird Montag, 23. September 2013 19:39
- Als Antwort markiert Marcel RomaModerator Dienstag, 24. September 2013 09:10
- Tag als Antwort aufgehoben IngoManthey Dienstag, 24. September 2013 19:44
-
Hallo zusammen,
Die Frage wurde anscheinend mehrfach und in verschiedenen Foren gepostet. Viele interessante Lösungsvorschläge wurden unterbreitet, weshalb mir nicht ganz klar ist, wo das Problem liegt. Auf codekicker z.B. hatte Maria Simlinger am 12.09 vorgeschlagen, dass man der CreateAttribute-Methode die Namespace-URI direkt mitgibt, was tatsächlich funktioniert:
XmlElement rootNode = doc.CreateElement("Document"); XmlElement xRootElement = (XmlElement)doc.AppendChild(rootNode); xRootElement.SetAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:pain.001.002.03"); xRootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlAttribute schemaLocationAttribute = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); schemaLocationAttribute.Value = "urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd"; xRootElement.Attributes.Append(schemaLocationAttribute);
Hier ist die XML-Ausgabe von doc.OuterXml:
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd" />
Gruß
Marcel
- Als Antwort markiert IngoManthey Dienstag, 24. September 2013 21:44
-
Hallo Marcel,
jetzt habe ich mein Problem endlich gefunden. Ich habe mit die XML Datei immer im IE angesehen.
Dort steht nicht:
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.002.03 pain.001.002.03.xsd" />sonder:
<Document xsi:schemaLocation=".......
Jetzt habe ich mir die Datei endlich in Editor angesehen und es ist alles richtig. Mir war nicht bekannt, das der IE was anderes anzeigt als in der Datei steht.
Danke für die Hilfe.
Grüße Ingo