खोज…


एक XML दस्तावेज़ उत्पन्न करें

लक्ष्य निम्नलिखित XML दस्तावेज़ उत्पन्न करना है:

<FruitBasket xmlns="http://www.fruitauthority.fake">
  <Fruit ID="F0001">
    <FruitName>Banana</FruitName>
    <FruitColor>Yellow</FruitColor>
  </Fruit>
  <Fruit ID="F0002">
    <FruitName>Apple</FruitName>
    <FruitColor>Red</FruitColor>
  </Fruit>
</FruitBasket>

कोड:

XNamespace xns = "http://www.fruitauthority.fake";
XDeclaration xDeclaration = new XDeclaration("1.0", "utf-8", "yes");
XDocument xDoc = new XDocument(xDeclaration);
XElement xRoot = new XElement(xns + "FruitBasket");
xDoc.Add(xRoot);

XElement xelFruit1 = new XElement(xns + "Fruit");
XAttribute idAttribute1 = new XAttribute("ID", "F0001");
xelFruit1.Add(idAttribute1);
XElement xelFruitName1 = new XElement(xns + "FruitName", "Banana");
XElement xelFruitColor1 = new XElement(xns + "FruitColor", "Yellow");
xelFruit1.Add(xelFruitName1);
xelFruit1.Add(xelFruitColor1);
xRoot.Add(xelFruit1);

XElement xelFruit2 = new XElement(xns + "Fruit");
XAttribute idAttribute2 = new XAttribute("ID", "F0002");
xelFruit2.Add(idAttribute2);
XElement xelFruitName2 = new XElement(xns + "FruitName", "Apple");
XElement xelFruitColor2 = new XElement(xns + "FruitColor", "Red");
xelFruit2.Add(xelFruitName2);
xelFruit2.Add(xelFruitColor2);
xRoot.Add(xelFruit2);

XML फ़ाइल को संशोधित करें

कोई XML फ़ाइल को संशोधित करने के XDocument , आप प्रकार का एक चर में फ़ाइल लोड XDocument , स्मृति में इसे संशोधित, फिर इसे सहेजें, मूल फ़ाइल अधिलेखित। एक सामान्य गलती XML को स्मृति में संशोधित करना और डिस्क पर फ़ाइल को बदलने की अपेक्षा करना है।

XML फ़ाइल दी गई:

<?xml version="1.0" encoding="utf-8"?>
<FruitBasket xmlns="http://www.fruitauthority.fake">
  <Fruit>
    <FruitName>Banana</FruitName>
    <FruitColor>Yellow</FruitColor>
  </Fruit>
  <Fruit>
    <FruitName>Apple</FruitName>
    <FruitColor>Red</FruitColor>
  </Fruit>
</FruitBasket>

आप केले के रंग को भूरा करना चाहते हैं:

  1. हमें डिस्क पर फ़ाइल का पथ जानने की आवश्यकता है।
  2. XDocument.Load का एक अधिभार एक URI (फ़ाइल पथ) प्राप्त करता है।
  3. चूंकि xml फ़ाइल एक नाम स्थान का उपयोग करती है, इसलिए हमें नाम स्थान और तत्व नाम के साथ क्वेरी करनी चाहिए।
  4. अशक्त मूल्यों की संभावना को समायोजित करने के लिए एक लाइनक क्वेरी C # 6 सिंटैक्स का उपयोग करती है। हर . यदि कोई तत्व नहीं मिलता है, तो इस क्वेरी में उपयोग की जाने वाली अशक्त सेट को वापस करने की क्षमता है। C # 6 से पहले आप इसे कई चरणों में करेंगे, रास्ते में अशक्तता की जाँच करेंगे। इसका परिणाम है <Fruit> तत्व जिसमें केले होते हैं। वास्तव में एक IEnumerable<XElement> , जिसके कारण अगला चरण FirstOfDefault() का उपयोग करता है।
  5. अब हम फ्रूटचोर तत्व को उस फ्रूट एलिमेंट से निकालते हैं जो हमने अभी पाया है। यहाँ हम मानते हैं कि सिर्फ एक है, या हम केवल पहले वाले की परवाह करते हैं।
  6. यदि यह अशक्त नहीं है, तो हम फ्रूटकोलर को "ब्राउन" पर सेट करते हैं।
  7. अंत में, हम XDocument सहेजते हैं, डिस्क पर मूल फ़ाइल को ओवरराइट करते हैं।
// 1.
string xmlFilePath = "c:\\users\\public\\fruit.xml";

// 2.
XDocument xdoc = XDocument.Load(xmlFilePath);

// 3.
XNamespace ns = "http://www.fruitauthority.fake";

//4. 
var elBanana = xdoc.Descendants()?.
    Elements(ns + "FruitName")?.
    Where(x => x.Value == "Banana")?.
    Ancestors(ns + "Fruit");

// 5.
var elColor = elBanana.Elements(ns + "FruitColor").FirstOrDefault();

// 6.
if (elColor != null)
{
    elColor.Value = "Brown";
}

// 7.
xdoc.Save(xmlFilePath);

फ़ाइल अब इस तरह दिखती है:

<?xml version="1.0" encoding="utf-8"?>
<FruitBasket xmlns="http://www.fruitauthority.fake">
  <Fruit>
    <FruitName>Banana</FruitName>
    <FruitColor>Brown</FruitColor>
  </Fruit>
  <Fruit>
    <FruitName>Apple</FruitName>
    <FruitColor>Red</FruitColor>
  </Fruit>
</FruitBasket>

धाराप्रवाह सिंटैक्स का उपयोग करके एक XML दस्तावेज़ बनाएं

लक्ष्य:

<FruitBasket xmlns="http://www.fruitauthority.fake">
  <Fruit>
    <FruitName>Banana</FruitName>
    <FruitColor>Yellow</FruitColor>
  </Fruit>
  <Fruit>
    <FruitName>Apple</FruitName>
    <FruitColor>Red</FruitColor>
  </Fruit>
</FruitBasket>

कोड:

XNamespace xns = "http://www.fruitauthority.fake";
XDocument xDoc = 
    new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
        new XElement(xns + "FruitBasket",
            new XElement(xns + "Fruit",
                new XElement(xns + "FruitName", "Banana"),
                new XElement(xns + "FruitColor", "Yellow")),
            new XElement(xns + "Fruit",
                new XElement(xns + "FruitName", "Apple"),
                new XElement(xns + "FruitColor", "Red"))
                ));


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow