在C#中使用LINQ在运行时更改XElement的默认值。

huangapple go评论56阅读模式
英文:

Change the default value of XElement at runtime in C# using LINQ

问题

要在C#中使用LINQ更改以下XElement的默认值,您可以使用以下代码:

XElement automobile = new XElement("Automobile",
    new XElement("MainBlock", "Car"),
    new XElement("Name", "Audi"),
    new XElement("Value",
        new XAttribute("type", "System.Double"),
        new XAttribute("min", "0"),
        new XAttribute("max", "100"),
        new XAttribute("default", "20"),  // Change the default value here
        new XAttribute("resolution", "1.0"),
        new XAttribute("unit", "")
    )
);

这将创建一个新的XElement,其中“default”属性的值已更改为20。您可以将此新XElement插入到文档中,或者使用它来替换现有的XElement。

英文:

How can I change the default value of the following XElement in C# using LINQ:

<Automobile>
  <MainBlock>Car</MainBlock>
  <Name>Audi</Name>
  <Value> type="System.Double" min="0" max="100" default="50" resolution="1.0" unit=""</Value>
</Automobile>

The default value is 50. I want to change it to 20.

答案1

得分: 1

以下是您要翻译的代码部分:

你有很多解决方案来做到这一点。一个解决方案:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(@"
<Automobile>
  <MainBlock>Car</MainBlock>
  <Name>Audi</Name>
  <Value type=""System.Double"" min=""0"" max=""100"" default=""50"" resolution=""1.0"" unit="""""></Value>
</Automobile>");

XmlNode sNode = xmldoc.SelectSingleNode("/Automobile/Value");
XmlAttribute defautAttribute = sNode.Attributes["default"];

if (defautAttribute != null)
    defautAttribute.Value = "20";

请注意,代码中的注释部分(// 后的文字)不需要翻译。

英文:

you have lot of solutions to do that. One solution:

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(@&quot;
        &lt;Automobile&gt;
          &lt;MainBlock&gt;Car&lt;/MainBlock&gt;
          &lt;Name&gt;Audi&lt;/Name&gt;
          &lt;Value type=&quot;&quot;System.Double&quot;&quot; min=&quot;&quot;0&quot;&quot; max=&quot;&quot;100&quot;&quot; default=&quot;&quot;50&quot;&quot; resolution=&quot;&quot;1.0&quot;&quot; unit=&quot;&quot;&quot;&quot;&gt;&lt;/Value&gt;
        &lt;/Automobile&gt;&quot;);

        XmlNode sNode = xmldoc.SelectSingleNode(&quot;/Automobile/Value&quot;);
        XmlAttribute defautAttribute = sNode.Attributes[&quot;default&quot;];

        if(defautAttribute != null)
            defautAttribute.Value = &quot;20&quot;;

答案2

得分: 0

这是一个 LINQ to XML 实现。

c#

void Main()
{
    XDocument xdoc = XDocument.Parse(@"<Automobile>
        <MainBlock>Car</MainBlock>
        <Name>Audi</Name>
        <Value type='System.Double' min='0' max='100' default='50'></Value>
    </Automobile>");

    xdoc.Element("Automobile").Element("Value").Attribute("default").SetValue("20");
}
英文:

Here is a LINQ to XML implementation.

c#

void Main()
{
	XDocument xdoc = XDocument.Parse(@&quot;&lt;Automobile&gt;
          &lt;MainBlock&gt;Car&lt;/MainBlock&gt;
          &lt;Name&gt;Audi&lt;/Name&gt;
          &lt;Value type=&#39;System.Double&#39; min=&#39;0&#39; max=&#39;100&#39; default=&#39;50&#39;&gt;&lt;/Value&gt;
        &lt;/Automobile&gt;&quot;);
	
	xdoc.Element(&quot;Automobile&quot;).Element(&quot;Value&quot;).Attribute(&quot;default&quot;).SetValue(&quot;20&quot;);
}

huangapple
  • 本文由 发表于 2023年2月14日 00:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定