簡體   English   中英

在C#中的標簽內生成帶有屬性的XML

[英]Generate XML with attributes within the tags in c#

我有以下XML結構。

<a>
  <b>
    <c>
      <d>54</d>
      <e>US</e>
      <f>Canada</f>          
      <g>
        <h>
          <i1>39</i1>              
          <i2>Belgium</i2>
          <i3>Russia</i3>
        </h>
        <h>
          <i1>43</i1>              
          <i2>Fran</i2>
          <i3>Ger</i3>
        </h>
      </g>
    </c>
    <c>
      <d>5</d>
      <e>US</e>
      <f>Can</f>          
      <g>
        <h>
          <i1>29</i1>              
          <i2>Brit</i2>
          <i3>Ice</i3>
        </h>
        <h>
          <i1>95</i1>              
          <i2>Russia</i2>
          <i3>Nero</i3>
        </h>
        <h>
          <i1>4323</i1>              
          <i2>Polska</i2>
          <i3>503</i3>
        </h>
      </g>
    </c>         
  </b>
  <b2>
    <c2>
      <d2>551</d2>
      <d3>Indo</d3>
      <e2>
        <f2>
          <g2>Irnna</g2>
          <g3>Mehak</g3>
          <g4>Vodka</g4>              
          <h2>
            <i2>
              <j1>44</j1>                  
              <j6>Germ0</j6>                  
            </i2>
            <i21>Finish</i21>
        </f2>    
      </e2>  
      <f3>544</f3>
      <g3>fur</g3>
      <h3>denmark</h3>
     <k1>lur</k1>
     <k2>Bern</k2>
     <k3>Zurick</k3>
     <k4>Italy</k4>
    </c2>
  </b2>
 <b3>35</b3>
 <b4>ferha</b4>
 <b5>english</b5>
 <b6>334</b6>
</a>

我需要像下面這樣轉換它-

<a>
      <b>
        <c d="54"; e="US"; f="Canada">        
         <g>
            <h i1="39"; i2 = "Belgium"; i3 ="Russia">               
            <h i1="393"; i2 = "Fran"; i3 ="Ger">                
         </g>
        </c>
        <c>
       .
       .
     .
    .

這意味着將所有屬性放入標簽中。 我使用了以下C#代碼-

var xDoc = XDocument.Load("my.xml");
                var xNode =
                    new XElement("a",
                        from name in xDoc.Root.Elements()
                        select new XElement(name.Name,
                            from names in name.Elements()
                            select new XElement(names.Name,
                                from namesElement in names.Elements()
                                where namesElement.Name.LocalName != "g"
                                select new XAttribute(namesElement.Name.LocalName, (string)namesElement),
                                new XElement("g",
                                    from Parts in names.Element("g").Elements()
                                    select new XElement(Parts.Name,
                                        from routeElement in Parts.Elements()
                                        select new XAttribute(routeElement.Name.LocalName, (string)routeElement)))))); 

但是,我遇到了錯誤。 您能給我任何提示或更正我的代碼有什么問題嗎? 實際上,它一直有效到(上半年)。 當結構在下半部分更改時,將顯示錯誤,例如“對象引用未設置為對象的實例。”,“重復屬性”。 你能告訴我我的方法是否正確嗎? 邏輯結構有什么問題嗎?

您的樣本輸入XML文檔缺少h2元素的結束標記。 另外,輸入XML文檔中的值與預期輸出XML中的值不匹配。 話雖如此,我使用了示例輸入(我添加了缺少的h2關閉標記)並通過以下代碼運行了它:

XDocument xDoc = XDocument.Load("my.xml");
XElement[] elements = xDoc.DescendantNodes()
    .Where(n => n.NodeType == XmlNodeType.Text).Select(n => n.Parent).ToArray();

foreach (XElement element in elements)
{
    element.Parent.SetAttributeValue(element.Name, element.Value);
    element.Remove();
}

結果輸出是這樣的:

<a b3="35" b4="ferha" b5="english" b6="334">
  <b>
    <c d="54" e="US" f="Canada">
      <g>
        <h i1="39" i2="Belgium" i3="Russia" />
        <h i1="43" i2="Fran" i3="Ger" />
      </g>
    </c>
    <c d="5" e="US" f="Can">
      <g>
        <h i1="29" i2="Brit" i3="Ice" />
        <h i1="95" i2="Russia" i3="Nero" />
        <h i1="4323" i2="Polska" i3="503" />
      </g>
    </c>
  </b>
  <b2>
    <c2 d2="551" d3="Indo" f3="544" g3="fur" h3="denmark" k1="lur" k2="Bern" k3="Zurick" k4="Italy">
      <e2>
        <f2 g2="Irnna" g3="Mehak" g4="Vodka">
          <h2 i21="Finish">
            <i2 j1="44" j6="Germ0" />
          </h2>
        </f2>
      </e2>
    </c2>
  </b2>
</a>

這似乎是您想要的東西,但是如果沒有,代碼可以為您指明正確的方向。

我使用的是LINQ方法語法而不是您使用的查詢語法,但是我認為那部分不重要。 但是我注意到您正在尋找特定的元素,例如“ a”或“ g”,並且我想看看是否可以用更一般的方式來完成。

在撰寫本文時,我也遇到了空引用異常。 例如,我最初將elements變量作為IEnumerable<XElement> ,但是后來我將在foreach從其父級中刪除一個元素,並且在下一次迭代中將有一些LINQ延遲執行,因此它將返回DescendantNodes()但在刪除該元素后無法正常工作,我會看到null引用異常。 因此,我不再使用LINQ更改內容,而是使用ToArray()集合,以便所有執行立即進行,而我使用數組。 我提到所有這一切,因為也許您也正在發生這種事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM