K
K
Ksenya232014-09-13 17:28:24
C++ / C#
Ksenya23, 2014-09-13 17:28:24

Finding the last child element and adding the next one in the xml document, error in logic?

By experience: for all item elements, insert a track element if the last of the child elements of item = title. Everything seems to be logical, but the elements with the name item were not found.
XML view:

<?xml version="1.0" encoding="utf-8"?>
    <!-- This is a SCORM 1.2 manifest file, created 9/1/2014 1:52:58 PM by semina using CourseLab 140211 -->
    <manifest xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd" identifier="MANIFEST-5D060C55_4EC0_4E49_8D2F_F5C934FFEDAD" version="1.0">
      <title>Технолог подсистемы САДД БР. Работа с модулем «Справочники»</title>
      <item identifier="im_1" identifierref="RES1" parameters="?width=1152&amp;height=920">
        <title>Информационный материал</title>
      </item>
      <item identifier="im_1" identifierref="RES1" parameters="?width=1152&amp;height=920">
        <title>Информационный материал</title>
      </item>
    </manifest>

By work:
XmlDocument data = new XmlDocument();
        string path = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\bin\Debug", @"");
            path = path + "imsmanifest.xml";
            XDocument data = XDocument.Load(path);

            XElement track = new XElement("track",
              new XAttribute("id", ""),
              new XAttribute("genre", "Break Beat"));

            IEnumerable<XElement> itemList = data.Elements("item");
          
            foreach (XElement el in itemList) {
              var childList = el.Elements();
              if (childList.Last().Name == "title") {
                 el.Add(track);
          }
        }
        data.Save(path);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aush, 2014-09-13
@Ksenya23

Elements are not found due to namespaces. I am not an expert in this, working with xml with namespaces has always seemed to me some kind of mysticism, but this is how it works:

var xml = XDocument.Parse(
@"<?xml version='1.0' encoding='utf-8'?>
<!-- This is a SCORM 1.2 manifest file, created 9/1/2014 1:52:58 PM by semina using CourseLab 140211 -->
<manifest xmlns='http://www.imsproject.org/xsd/imscp_rootv1p1p2' xmlns:adlcp='http://www.adlnet.org/xsd/adlcp_rootv1p2' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation=' http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd' identifier='MANIFEST-5D060C55_4EC0_4E49_8D2F_F5C934FFEDAD' version='1.0'>
  <title>Технолог подсистемы САДД БР. Работа с модулем «Справочники»</title>
  <item identifier='im_1' identifierref='RES1' parameters='?width=1152&amp;height=920'>
    <title>Информационный материал</title>
  </item>
  <item identifier='im_1' identifierref='RES1' parameters='?width=1152&amp;height=920'>
    <title>Информационный материал</title>
  </item>
</manifest>");

var ns = xml.Root.GetDefaultNamespace();

foreach (var element in xml.Root.Descendants().Where(e => e.Name == ns + "item"))
{
    var last = element.Elements().Last();
    if (last.Name == ns + "title")
    {
        last.AddAfterSelf(
            new XElement("track", 
                new XAttribute("id", ""), 
                new XAttribute("genre", "Break Beat")));
    }
}

K
Ksenya23, 2014-09-14
@Ksenya23

I want to supplement the topic with a question about namespace. If you need to replace the attributes of the manifest element:

<manifest   xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" 
                xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
                xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd 
                                                [url]http://www.imsglobal.org/xsd/imsmd_rootv1p2p1[/url] imsmd_rootv1p2p1.xsd"
>

string text = System.IO.File.ReadAllText(@"C:\\Users\\Ксюша\\Desktop\\1_2\\11.xml");
            var xml = XDocument.Parse(text);
            var ns = xml.Root.GetDefaultNamespace();
 
            XNamespace xci = "http://www.w3.org/2001/XMLSchema-instance";
 
            string srt = "";
            string[] xciValue = new string[]{"http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd", 
                                                "http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd"};
            for (int i = 0; i < (xciValue.Count()); i++){
                srt = srt + xciValue[i] + " ";
            }
            xml.Root.ReplaceAttributes( 
                     new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                     new XAttribute(xci + "schemaLocation", srt)
                );
xml.Save("C:\\Users\\Ксюша\\Desktop\\1_2\\11.xml");

- based on example No. 2: msdn.microsoft.com/ru-ru/libr...v=vs.110).aspx
Instead, we get an error on the line: var xml = XDocument.Parse(text);
Prefix "p1" undeclared., line 2, position 11.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question