标签: xmldocument

如何处置XmlDocument

我正在使用流创建 XmlDocument,并在 XmlDocument 中进行一些更改,并将 XmlDocument 保存到流本身。

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(fileStream);

////
////

////  
xmlDocument.Save(fileStream);
//how to dispose the created XmlDocument object.
Run Code Online (Sandbox Code Playgroud)

现在我如何销毁 XmlDocument 对象?

.net c# xmldocument

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

在.Net Core 3.1 Web Api项目中使用System.Text.Json将XmlDocument序列化为Json

我正在 .net Core 3.1 Web api 项目中从 Newtonsoft.Json 切换到 System.Text.Json。该项目是一个遗留的 .net core Web api 项目,拥有数百个客户端。某些控制器端点返回 XmlDocuments。使用 Newtonsoft.Json 将 XmlDocuments 序列化为 Json 工作正常并给出了预期结果。不幸的是,当切换到 System.Text.Json 时,序列化的工作方式有所不同。

例子 ;使用 System.Xml;

public class Program
{
    public static void Main()
    {
        var xml = @"<root><items><item id=""1"">item 1</item><item id=""2"">item 2</item></items></root>";
        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);        
        Console.WriteLine($"Xml: {xml}");
        Console.WriteLine($"XmlDocument xml: {xmlDoc.ToString()}");
        
        var serializedXml = System.Text.Json.JsonSerializer.Serialize(xmlDoc);
        Console.WriteLine($"Serialized xml using System.Text.Json: {serializedXml}");
        
        serializedXml = Newtonsoft.Json.JsonConvert.SerializeObject(xmlDoc);
        Console.WriteLine($"Serialized xml using Newtonsoft.Json: {serializedXml}");
    }
}
Run Code Online (Sandbox Code Playgroud)

.net 小提琴手示例

结果

Xml: <root><items><item id="1">item …
Run Code Online (Sandbox Code Playgroud)

c# xmldocument json.net system.text.json asp.net-core-3.1

5
推荐指数
0
解决办法
6142
查看次数

ASP.NET:将一个XmlDocument保存到Response.OutputStream是否符合编码?

我想将一个XmlDocument对象的xml发送到HTTP客户端,但我担心建议的soltuion可能不会遵守Response已设置使用的编码:

public void ProcessRequest(HttpContext context)
{
   XmlDocument doc = GetXmlToShow(context);

   context.Response.ContentType = "text/xml";
   context.Response.ContentEncoding = System.Text.Encoding.UTF8;
   context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
   context.Response.Cache.SetAllowResponseInBrowserHistory(true);

   doc.Save(context.Response.OutputStream);
Run Code Online (Sandbox Code Playgroud)

}

如果我将编码更改为其他内容,例如Unicode,该怎么办?

public void ProcessRequest(HttpContext context)
{
   XmlDocument doc = GetXmlToShow(context);

   context.Response.ContentType = "text/xml";
   context.Response.ContentEncoding = System.Text.Encoding.Unicode;
   context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
   context.Response.Cache.SetAllowResponseInBrowserHistory(true);

   doc.Save(context.Response.OutputStream);
}
Run Code Online (Sandbox Code Playgroud)

请问Response.OutputStream翻译多数民众赞成被写入到它在运行中的二进制数据,并使其Unicode的?

或者Response.ContentEncoding只是提供信息

如果ContentEncoding只是提供信息,那么以下文本字符串将返回什么内容编码?

context.Response.ContentEncoding = System.Text.Encoding.Unicode;
context.Response.Write("Hello World");

context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write("Hello World");

context.Response.ContentEncoding = System.Text.Encoding.UTF16;
context.Response.Write("Hello World");

context.Response.ContentEncoding = System.Text.Encoding.ASCII;
context.Response.Write("Hello World");

context.Response.ContentEncoding = System.Text.Encoding.BigEndianUnicode; …
Run Code Online (Sandbox Code Playgroud)

xml asp.net unicode xmldocument

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

使用XSD验证XmlDocument

我正在开发一个系统,它将通过webservice接收XML(XmlDocument).我不会在硬盘上有这个XML(XmlDocument).它将在内存上进行管理.

我有一个文件XSD来验证我从WebService收到的XML(XmlDocument).我试图做一个例子来验证这个Xml.

我的XML:

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
Run Code Online (Sandbox Code Playgroud)

我也有我的XSD:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
  <xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:int"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

正如我们所看到的,我将身体字段设为int,只是为了模拟错误.

好吧,为了尝试获取错误,我有以下代码:

//event handler to manage the errors
private static void verifyErrors(object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Warning)
        MessageBox.Show(args.Message);
}
Run Code Online (Sandbox Code Playgroud)

点击按钮,我有:

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                // …
Run Code Online (Sandbox Code Playgroud)

xml validation xmldocument

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

哪个是性能最佳的:带XPath的XPathNavigator vs带有查询的Linq到Xml?

我有一个应用程序,我使用XPathNavigator迭代节点.它工作正常.

但我想知道如果我使用LINQ to Xml ....

  1. 我会得到什么好处(性能,可维护性)?

  2. 有了XPath,LINQ到Xml的性能如何?

我正在使用C#.net,VS 2010,我的.xml是中等大小.

c# xmldocument .net-4.0 linq-to-xml xpathnavigator

4
推荐指数
2
解决办法
6246
查看次数

如果没有DTD,XML DocumentType方法CreateDocumentType崩溃.NET C#

我在生成需要自动生成的XML然后手动上载到客户端站点以进行验证时遇到问题.

客户的规范要求我们在顶部包含DTD声明.那是

<!DOCTYPE TheFile System "TheDTD.dtd" ""> 
Run Code Online (Sandbox Code Playgroud)

因此,当我尝试生成XML行时,以编程方式

    XmlDocumentType doctype;
    doctype = doc.CreateDocumentType("TheFile", null, "TheDTD.dtd", null);
    doc.AppendChild(doctype);
Run Code Online (Sandbox Code Playgroud)

失败并且它问我"TheDTD.dtd"应该存在于windows目录中:C:\ Windows\System32\inetsrv

后来如果我想读取XmlDoc那一行

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlLocation);
Run Code Online (Sandbox Code Playgroud)

失败并且它询问我"TheDTD.dtd"应该存在于存储整个项目的Project目录中

我不想实际验证DTD,我只是想引用,因为验证发生在客户端.有没有办法在XML中添加行""而不必在两个不同的位置复制实际的.dtd?

如果我们公开声明并将其更改为

 <!DOCTYPE TheFile PUBLIC "TheDTD.dtd" ""> 
Run Code Online (Sandbox Code Playgroud)

然后一切正常,但客户要求它应该是SYSTEM而不是PUBLIC.我希望如果实际的.dtd丢失,CreateDocumentType和Load方法不会崩溃,因为此时我甚至没有这个文件而且我的目的不需要它.

谢谢

c# xml doctype xmldocument dtd

4
推荐指数
1
解决办法
3466
查看次数

是否有来自.NET的XmlDocument.LoadXml()的Java等价物?

.NETC#中,当尝试加载stringinto时xml,需要使用XmlDocumenttype from System.Xml并执行以下操作:

例如:

string xmlStr = "<name>Oscar</name>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
Console.Write(doc.OuterXml);
Run Code Online (Sandbox Code Playgroud)

这看起来很简单,但我该怎么办Java呢?是否可以加载stringxml直接使用的东西,简短的像上面,避免执行这个其他方法?

提前致谢.

.net c# java xml xmldocument

4
推荐指数
1
解决办法
7249
查看次数

从字符串xml中获取节点值

我有这个字符串XML

string innerXml = @"<detail><WCFFaultExcepcion xmlns=""http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId><Message>Índice fuera de los límites de la matriz.</Message></WCFFaultExcepcion></detail>";
Run Code Online (Sandbox Code Playgroud)

这是stringXML

<detail>
    <WCFFaultExcepcion xmlns="http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId>
        <Message>Índice fuera de los límites de la matriz.</Message>
    </WCFFaultExcepcion>
</detail>
Run Code Online (Sandbox Code Playgroud)

我想要的是获取详细信息Tag的值,我正在尝试这个例子,但所有返回null o cero count,你能帮帮我吗?

 private static void Example()
        {
            string innerXml = @"<detail><WCFFaultExcepcion xmlns=""http://schemas.datacontract.org/2004/07/CIEL.DigiturnoMega.Entidades"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><ErrorId>b7e9d385-9118-4297-baca-db9ab00f3856</ErrorId><Message>Índice fuera de los límites de la matriz.</Message></WCFFaultExcepcion></detail>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(innerXml);

            XmlNode node = (XmlNode)doc.DocumentElement;
            XmlNode optionalNode = node.SelectSingleNode("/detail/WCFFaultExcepcion");
            XmlNode optionalNode1 = node.SelectSingleNode("detail/WCFFaultExcepcion");
            XmlNode optionalNode2 = node.SelectSingleNode("/detail/WCFFaultExcepcion/ErrorId");
            XmlNode optionalNode3 = node.SelectSingleNode("detail/WCFFaultExcepcion/ErrorId");
            XmlElement optional …
Run Code Online (Sandbox Code Playgroud)

c# xmldocument

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

如何使用新值更新XML节点?

我的App_Data文件夹里面有一个xml .我需要编辑该xml节点中的值.我试过的是 -

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("~/App_Data/conf.xml.config"));

        XmlNodeList aNodes = xDoc.SelectNodes("/ConfigInf");
        foreach (XmlNode node in aNodes)
        {
            XmlNode child1 = node.SelectSingleNode("Node1");
            XmlNode child2 = node.SelectSingleNode("Node2");              

            child1.InnerText = "Value1";
            child2.InnerText = "Value2";
        }
Run Code Online (Sandbox Code Playgroud)

我需要用新值重新编写xml,因为当我尝试再次访问相同的xml时,它应该包含新值.但是当我访问xml时,我仍然只在我这样调用时得到旧的(初始)值 - Test.LoadConf(Server.MapPath("./App_Data/conf.xml.config"));.如何使用新值或任何替代方法(如使用新值创建新的xml)来写入XML?(因为我只需要在单个页面中访问此xml)

c# xml asp.net xmldocument xmlnode

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

C#从xml属性中获取值

如何使用C#以正确的方式获取属性"action"和"filename"值?

XML:

<?xml version="1.0" encoding="utf-8" ?>
 <Config version="1.0.1.1" >
   <Items>
    <Item action="Create" filename="newtest.xml"/>
    <Item action="Update" filename="oldtest.xml"/>   
  </Items>
 </Config>
Run Code Online (Sandbox Code Playgroud)

C#:我无法获取属性值以及如何获取foreach循环中的值?怎么解决这个?

        var doc = new XmlDocument();
        doc.Load(@newFile);
        var element = ((XmlElement)doc.GetElementsByTagName("Config/Items/Item")[0]); //null
        var xmlActions = element.GetAttribute("action"); //cannot get values
        var xmlFileNames= element.GetAttribute("filename"); //cannot get values

         foreach (action in xmlActions)
         {
           //not working
         }

         foreach (file in xmlFileNames)
         {
           //not working
         }
Run Code Online (Sandbox Code Playgroud)

您的代码示例对我来说意味着很多.谢谢!

c# xml xmldocument

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