标签: xmldocument

如何以编程方式修改app.config中的assemblyBinding?

我试图通过使用XmlDocument类并直接修改值来在安装时更改bindingRedirect元素.这是我的app.config看起来像:

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">            
            ...
        </sectionGroup>      
    </configSections>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="MyDll" publicKeyToken="31bfe856bd364e35"/>
          <bindingRedirect oldVersion="0.7" newVersion="1.0"/>
        </dependentAssembly>
     </assemblyBinding>
    </runtime>    
...
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下代码将1.0更改为2.0

private void SetRuntimeBinding(string path, string value)
{
    XmlDocument xml = new XmlDocument();

    xml.Load(Path.Combine(path, "MyApp.exe.config"));
    XmlNode root = xml.DocumentElement;

    if (root == null)
    {
        return;
    }

    XmlNode node = root.SelectSingleNode("/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect/@newVersion");

    if (node == null)
    {
        throw (new Exception("not found"));
    }

    node.Value = value;

    xml.Save(Path.Combine(path, "MyApp.exe.config"));
}
Run Code Online (Sandbox Code Playgroud)

但是,它会引发"未找到"的异常.如果我将路径备份到/ configuration/runtime它就可以了.但是,一旦我添加了assemblyBinding,它就找不到该节点.可能这与xmlns有关吗?知道怎么修改这个吗?ConfigurationManager也无权访问此部分.

c# xml configuration xmldocument configuration-files

10
推荐指数
2
解决办法
1万
查看次数

在C#.net中使用xmldocument添加根元素

我需要在C#中使用xmldocument对象创建XML文件.

如何添加如下的根元素:

 book:aaaa xsi:schemalocationchemaLocation="http://www.com"
Run Code Online (Sandbox Code Playgroud)

c# xmldocument

9
推荐指数
1
解决办法
1万
查看次数

GetElementsByTagName不区分大小写?

我正在使用GetElementsByTagName从xml中提取元素.GetElementsByTagName区分大小写 - 如果节点名称为"PARAMS"而不是"Params",则会引发异常.我不想这样,我可以在XMLDocument中使用不同的方式,因此它不会区分大小写吗?

xml xmldocument case-sensitive getelementsbytagname

9
推荐指数
1
解决办法
5171
查看次数

ImportNode创建空xmlns属性

重新代码:

   var tmpNewNode = xdoc.ImportNode(newNode, true);

    if (oldNode.ParentNode != null)
    {
        oldNode.ParentNode.ReplaceChild(tmpNewNode, oldNode);
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

使用空xmlns属性(xmlns ="")创建tmpNewNode.有什么建议我怎么能避免呢?

10倍

.net c# xml xmldocument

9
推荐指数
1
解决办法
4576
查看次数

C#从XML中提取数据

我正在尝试从URL中读取XML中的天气数据.XML看起来像这样:

<weatherdata>
<location>...</location>
<credit>...</credit>
<links>...</links>
<meta>...</meta>
<sun rise="2013-05-11T04:49:22" set="2013-05-11T21:39:03"/>
<forecast>
<text>...</text>
<tabular>
<time from="2013-05-11T01:00:00" to="2013-05-11T06:00:00" period="0">
<!--
 Valid from 2013-05-11T01:00:00 to 2013-05-11T06:00:00 
-->
<symbol number="2" name="Fair" var="mf/02n.03"/>
<precipitation value="0" minvalue="0" maxvalue="0.1"/>
<!--  Valid at 2013-05-11T01:00:00  -->
<windDirection deg="173.8" code="S" name="South"/>
<windSpeed mps="4.2" name="Gentle breeze"/>
<temperature unit="celsius" value="9"/>
<pressure unit="hPa" value="1004.2"/>
</time>
</tabular>
</forecast>
<observations>...</observations>
</weatherdata>
Run Code Online (Sandbox Code Playgroud)

我对XML中的预测数据感兴趣.我想得到时间和时间,然后是天气数据.例如,温度在XML中如下所示:

<temperature unit="celsius" value="9"/>
Run Code Online (Sandbox Code Playgroud)

我想用这样的东西提取数据:

 string fromTime = time from(the attribute in the xml);
                     string fromTime =time to(the attribute in the xml);
                    string name = …
Run Code Online (Sandbox Code Playgroud)

.net c# xml xmldocument xml-parsing

9
推荐指数
2
解决办法
3万
查看次数

如何使用XmlDocument/XmlDeclaration添加自定义XmlDeclaration?

我想在c#.net 2或3中使用XmlDocument/XmlDeclaration类时创建自定义XmlDeclaration.

这是我想要的输出(这是第三方应用程序的预期输出):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?MyCustomNameHere attribute1="val1" attribute2="val2" ?>
[ ...more xml... ]
Run Code Online (Sandbox Code Playgroud)

使用XmlDocument/XmlDeclaration类,看起来我只能创建一个带有一组已定义参数的XmlDeclaration:

XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "ISO-8859-1", null);
doc.AppendChild(declaration);
Run Code Online (Sandbox Code Playgroud)

是否有一个除了XmlDocument/XmlDeclaration之外的类,我应该看一下创建自定义XmlDeclaration?或者有没有办法使用XmlDocument/XmlDeclaration类本身?

.net c# xml xmldocument

8
推荐指数
2
解决办法
2万
查看次数

XML解析错误 - C#

我的代码在第7行第32位有一个XML解析错误,我不确定为什么

确切的错误转储

5/1/2010 10:21:42 AM
System.Xml.XmlException: An error occurred while parsing EntityName. Line 7, position 32.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, Int32 lineNo, Int32 linePos)
   at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos)
   at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
   at System.Xml.XmlTextReaderImpl.ParseAttributes()
   at System.Xml.XmlTextReaderImpl.ParseElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
   at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at System.Xml.XmlDocument.Load(String filename)
   at Lookoa.LoadingPackages.LoadingPackages_Load(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Lookoa\Lookoa\LoadingPackages.cs:line 30
Run Code Online (Sandbox Code Playgroud)

Xml文件,请注意这只是一个示例,因为我希望程序在我开始填充此存储库之前工作

<repo>
<Packages> …
Run Code Online (Sandbox Code Playgroud)

c# xml xmldocument visual-studio-2010

8
推荐指数
1
解决办法
7981
查看次数

XmlDocument.WriteTo截断结果文件

尝试将XmlDocument序列化为文件.XmlDocument相当大; 但是,在调试器中,我可以看到InnerXml属性中包含所有XML blob - 它没有被截断.

这是将我的XmlDocument对象写入文件的代码:

// Write that string to a file.
var fileStream = new FileStream("AdditionalData.xml", FileMode.OpenOrCreate, FileAccess.Write);
xmlDocument.WriteTo(new XmlTextWriter(fileStream, Encoding.UTF8) {Formatting = Formatting.Indented});
fileStream.Close();
Run Code Online (Sandbox Code Playgroud)

这里生成的文件只写入5,760行 - 它实际上是在标记中间截断的!

任何人都有任何想法为什么会在这里截断?

更新:我找到了问题的根源.在关闭文件流之前,我没有关闭XML Text Writer!D'哦!

c# xml xmldocument xmltextwriter filestream

8
推荐指数
1
解决办法
2339
查看次数

将XmlDocument插入XmlDocument节点

我用一个节点创建了一个基本的XmlDocument:

XmlDocument bigDoc = new XmlDocument();
bigDoc.LoadXml("<Request></Request>");
Run Code Online (Sandbox Code Playgroud)

我正在获取另一个我想在<Request>节点内插入的XmlDocument .它对我不起作用:

 XmlNode requestNode =  bigDoc.FirstChild;
 requestNode.AppendChild(anotherXMLDocument);
Run Code Online (Sandbox Code Playgroud)

它引发了一个例外.

如何在另一个XmlDocument节点中插入XmlDocument?

.net c# xmldocument insert xmlnode

8
推荐指数
1
解决办法
7535
查看次数

XmlDocument.Load失败,LoadXml工作:

在回答这个问题时,我遇到了一个我不理解的情况.OP正在尝试从以下位置加载XML:http://www.google.com/ig/api?weather = 12414&hl =

明显的解决方案是:

string m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml
Run Code Online (Sandbox Code Playgroud)

然而,这失败了

XmlException:给定编码中的字符无效.第1行,第499位.

这似乎是在窒息àUmidità.

OTOH,以下工作正常:

var m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
string xmlStr;
using(var wc = new WebClient())
{
    xmlStr = wc.DownloadString(m_strFilePath);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
Run Code Online (Sandbox Code Playgroud)

我为此感到困惑.任何人都可以解释为什么前者失败,但后者工作正常吗?

值得注意的是,文档的xml声明省略了编码.

c# xmldocument load xmlexception

8
推荐指数
1
解决办法
4507
查看次数