जोड़ता है मैं एक नई एक्सएमएल फ़ाइल बनाने के लिए लिंक से एक्सएमएल का उपयोग कर रहा हूं। फ़ाइल का कुछ हिस्सा मुझे मौजूदा एक्सएमएल फाइल से मिलता है। मैं इसके लिए निम्नलिखित कोड का उपयोग करता हूं।XElement एक xmlns
var v2 = new XDocument(
new XDeclaration("1.0", "utf-16", ""),
new XComment(string.Format("Converted from version 1. Date: {0}", DateTime.Now)),
new XElement(ns + "keyem",
new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(xsi + "schemaLocation", schemaLocation.NamespaceName),
new XAttribute("version", "2"),
new XAttribute("description", description),
new XElement(ns + "layout",
new XAttribute("type", type),
new XAttribute("height", height),
new XAttribute("width", width),
settings.Root) // XML from an existing file
समस्या यह है कि यह xmlns = मौजूदा फ़ाइल से पहले तत्व कहते हैं, "" है।
परिणाम है:
<?xml version="1.0" encoding="utf-16"?>
<foo
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
xmlns="http://tempuri.org/KeyEmFileSchema.xsd">
<settings xmlns="">
...
</settings>
</foo>
एक्सएमएल फ़ाइल मैं इस तरह दिखता है से पढ़ रहा हूँ, लेकिन अगर
<?xml version="1.0" encoding="utf-16"?>
<settings>
<colormaps>
<colormap color="Gray" textcolor="Black"/>
<colormap color="DarkGray" textcolor="White"/>
<colormap color="Black" textcolor="White"/>
<colormap color="Cyan" textcolor="Black"/>
</colormaps>
<macromaps>
<macromap pattern="^@([0-9A-F]{2})\|([0-9A-F]{2})$" replace="{ESC}$1{ESC}$2{MOUSERESET}"/>
<macromap pattern="^\$([0-9A-F]{2})\|([0-9A-F]{2})$" replace="{USERCLICK}{ESC}$1{ESC}$2{MOUSERESET}"/>
<macromap pattern="^\$([0-9A-F]{2})$" replace="{USERCLICK}{ESC}$1"/>
</macromaps>
<keydefault color="Cyan"/>
<groupdefault color="DarkGray"/>
</settings>
मैं समझता हूं, लेकिन मैं यह कैसे कर सकता हूं? मैंने डिफ़ॉल्ट सेटिंग्स की कोशिश की है। नाम = ns + defaultSettings.Name.LocalName; लेकिन मुझे इसे सभी उप-तत्वों के लिए करना होगा। कुछ बेहतर होना चाहिए। – magol
आपको या तो Xslt तकनीकों का उपयोग करके दस्तावेज़ को बदलने की आवश्यकता है, या प्रत्येक तत्व को पढ़ने और कोड में बदलने के लिए। असल में, आपके द्वारा लोड किया गया एक्स डॉक्यूमेंट उस दस्तावेज़ में प्रत्येक तत्व के नामस्थान को जानता है, और पता है कि यह foo के समान नामस्थान नहीं है। –
क्या मैं उस एक्सएमएल फ़ाइल को बदल सकता हूं जिसे मैंने सही नामस्थान में प्राप्त करने के लिए पढ़ा है? – magol