0

I am updating my xml grammar file from C#. I am successfully inserted child node within node. I want to insert child node with attribute. I have done this but format is wrong.I want to add new node named as and its attribute names ad having value like this out = "Karaa chee";.****

<?xml version="1.0" encoding="utf-8"?>
             <grammar xml:lang="en-US" root="top" mode="voice" tag-format="semantics/1.0"
             sapi:alphabet="x-microsoft-ups" 
       version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
      xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions">

       <rule id="top" scope="public">
       <item>
        <ruleref uri="#CityName"/>
        <tag> out = "src=" + rules.latest(); </tag>
       </item>
       to
       <item>
        <ruleref uri="#CityName"/>
        <tag> out += "^dst=" + rules.latest(); </tag>
      </item>
       </rule>

      <rule id="CityName" scope="private">
      <one-of>

       <item>
       Abbottabad
        <tag>out = "Abbott aabaad";</tag>
        </item>

       <item>
        Karachi
        <tag>out = "Karaa chee";</tag>
       </item>

         <item>
        New Item here
        <tag>out = "new item pronunciation here";</tag>
         </item>   

      </one-of>
     </rule>

    </grammar>

Here C# code that i used to add node and its attribute

XmlElement eleItem = xmlDoc.CreateElement("item", ns);
 eleItem.InnerText = item;
   eleItem.SetAttribute("tag", val);
    foundNode.AppendChild(eleItem);

3 Answers3

1

Like Panagiotis already commented:

your desired output is not valid

<item>
    New York
    <tag>out = "Neww Yarke";</tag>
</item>

your desired output should be like the following:

<item>
    New York
    <tag out="Neww Yarke"/>
</item>

you can get the above by doing the following using linq to XML

var document = new XDocument();
var itemelement = new XElement("item", 
    "New York",
    new Xelement("tag", new XAttribute("out", "Neww Yarke")));
Schuere
  • 1,579
  • 19
  • 33
  • 1
    There's no problem with having text and child elements inside another element. If it wasn't valid then XHTML wouldn't work. The OP's XML is perfectly valid. – Enigmativity Jul 01 '15 at 08:21
  • The code `new XElement("item", "New York", new XElement("tag", new XAttribute("out", "Neww Yarke")))` works just fine and produces `New York`. – Enigmativity Jul 01 '15 at 08:23
  • @Enigmativity, my bad, i'm quite frequently working with xml's and the tag out = "Neww Yarke"; didn't fit the attribute question, so I changed that. However I did add the value child as it's an habit to do so. I will edit the answer – Schuere Jul 01 '15 at 11:03
  • The spec here http://www.w3.org/TR/semantic-interpretation/#SI2 suggests that tags should be in the form out="Neww Yarke"; – Martin Brown Jul 03 '15 at 08:15
1

The XML given in the question shows that the item element can contain both text and further tag elements. Note that the tag as shown in the example XML is not an attribute which is what your code is trying to create, which would be written like this:

<item tag="out = &quot;Abbott aabaad&quot;;">Abbottabad</item>

What you are creating is child elements. Also intermixed with the elements are chunks of text. To create a mixed text and element content you need to use Text Nodes and Element Nodes like this:

public static void Main()
{
    XmlDocument doc = new XmlDocument();
    var root = doc.CreateElement("grammar");
    doc.AppendChild(root);

    var item = doc.CreateElement("item");

    var text = doc.CreateTextNode("Abbottabad");
    item.AppendChild(text);

    var tag = doc.CreateElement("tag");
    tag.InnerText = "out = \"Abbott aabaad\";";
    item.AppendChild(tag);

    root.AppendChild(item);

    Console.WriteLine(doc.OuterXml);
}

Which produces something like this:

<grammar>
    <item>
       Abbottabad
       <tag>out = "Abbott aabaad";</tag>
    </item>
</grammar>
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
1

With LINQ to XML, you can do this :

XNamespace ns = "http://www.w3.org/2001/06/grammar";
XDocument xmlDoc = XDocument.Load("xmlfile.xml");
XElement newitem = new XElement(ns +"Item", new Object[] {"New York", 
                                new XElement(ns + "tag", new Object[] {new XAttribute("out", "Neww Yarke")})});
        XElement parentNode = xmlDoc.Descendants(ns + "one-of").First();
        parentNode.Add(newitem);
        xmlDoc.Save("xmlfile.xml");

If you hesitate between XmlDocument or XDocument : topic

Community
  • 1
  • 1
Neyoh
  • 623
  • 1
  • 11
  • 33